-
Notifications
You must be signed in to change notification settings - Fork 2.3k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for .env files #337
Comments
I don't think this is in the scope of Poetry. Since Poetry is not configured via environment variables there are no reasons for it to load a If the libraries you use need it, it's their responsability to load it not Poetry's. |
I've always liked using direnv for this. It supports loading |
@jacebrowning direnv isn't cross platform unfortunately. When the environments I worked in were solely on Linux I used direnv in conjunction with vex to achieve functionality similar to I agree that poetry doesn't need .env files for it's own configuration (although it's not a bad idea to use them to allow for directory-specific configuration that may differ from the globals defined in the config file) but I don't think it's out of scope for poetry to load I understand the concerns about scope creep, especially when the PR adds a new dependency. If you're exclusively writing libraries then this particular use case may not occur very often but for 12 factor apps it's pretty vital to have tooling to support it- and it always seemed like a neat solution to me to have the virtualenvironment be in control of it. |
I've used https://github.com/jezdez/envdir/ for something similar. it uses an https://github.com/toddbluhm/env-cmd is also good if you want to stick to the it should be simple to use an alias that chains the two commands together |
Again, I'm aware other external libraries exist that do this but I'm of the belief that this particular functionality would be useful to have included in Poetry itself. It's a vital part of tooling required for 12 factor apps and it would be helpful to people who are moving towards adopting an environment variables-based approach in their project to have support for it in the tooling they (ideally) are already using for their project. The goal being to reduce the barrier to entry for managing python projects so that we don't end up back in the "oh just install pip and virtualenv and virtualenvwrapper, easy!" mess that we're finally moving away from :) If @sdispater would prefer that this isn't included within the project then my next solution would be to modify the entry-point to my Django applications so that they read the required env vars using |
I'd like to voice my support of this. The ability to automatically load .env files when running Environmental variables are generally designed to be read from the shell (i.e. not a file) and so it makes sense for this to be within the scope of poetry, given that it creates a sub shell for isolation. In short: Environmental variables should be part of Poetry's virtual environment. |
What's the problem with having direnv or aactivator or whatever handle this? To be honest, I also use them to handle the virtualenv creation/activation but that's purely optional. I prefer those dedicated projects, because I can also use them on non-python projects (e.g. to set up credentials for databases, environmental variables etc). I can understand that it could be convenient to have this implemented in poetry itself, but it feels like it is kind of out of scope + there are projects that do exactly this thing and IMHO quite well so. |
Could be a candidate for a plugin once they are a thing. |
The fact that setting up an environment on Widows is hard does not answer why setting up the environment becomes poetry's responsibility though. |
@pmav99 I've already stated why I think setting up the environment in poetry's virtual environment makes sense. Poetry uses virtual environments to provide isolation. That's the point of virtual environments. How does it not makes sense that within that environment, environmental variables could be set? This is particularly true for the You simply stated why I couldn't use other pieces of software, and I answered. Windows is not supported by them. Poetry, on the other hand, claims to support Windows. |
If the upcoming plugin system has hooks for injecting env vars then I agree that this would be an ideal candidate for a plugin |
oh boy, coming from pipenv, which has an array of issues, poetry seems to have issues which pipenv has solved. why dont they just marry already |
Like I said above this is beyond the scope of Poetry so I am closing this issue. |
So, I know this issue is closed, but can anyone give instructions on how to set the environmental variables on a |
I use a custom shell function, so I can do For example, Bash: withenv () {
env_file="$1"
cmd="${@:2}"
bash -c "source $env_file && $cmd"
} Fish: # Source: http://lewandowski.io/2016/10/fish-env/
function posix-source -d "Source a POSIX-formatted .env file"
for i in (cat $argv)
set arr (echo $i |tr = \n)
set -gx $arr[1] $arr[2]
end
end
function withenv -d "Run a command in a subshell with a .env file sourced"
set env_file $argv[1]
set cmd $argv[2..-1]
fish -c "posix-source $env_file; and $cmd"
end |
Hi @orn688 is it compatible with zsh ? |
It should be, just substitute |
I feel like the community is in strong disagreement about this issue... |
I'll also voice my meaningless disagreement with this decision. I came to poetry from pipenv because I wanted to simplify the toolkit I use - to spend less time maintaining and more time creating. This issue goes against the flow and poetry actually creates a problem that was already solved with pipenv. To paraphrase @sdispater: This issue was not closed because of a technical reason. |
Coming from |
You're right. The reason for closing it was not technical but because this feature does not align with the vision I have for Poetry. Believe me, I try to analyze every feature request made here to see if it matches the direction I want the project to go in and, if not, I reject the idea. It's as simple as that. I understand that this feature could be useful for some people, I do, however it's not the purpose of a dependency and package manager to manage the That being said, my idea has always been for Poetry to be extensible that's why I am working on a plugin system so that people can tailor it to there needs if they feel like what Poetry is doing by default is not enough. @rajasimon Sorry to see you go! I hope you'll change your mind in the future. |
This would be great, thank you! I'm also of the opinion that if you're offering automated shell creation with |
No one expects Poetry to manage the Having a vision is indeed a noble thing but also I assume that you are developing Poetry for people. When so many of us have a need for a particular feature there may be perhaps a genuine, logical reason? |
@sdispater Were |
@gormster I am failing to see your point:
So, to be clear, I don't understand what virtual environments have to do with environment variables. And to add to that, none of the package/dependency managers out there that I know of (beside So, let me ask you a question: before Poetry (and I will also reiterate what I said above:
If you want to go the 12-factor apps route, it's your responsibility to make your application load and use the environment variables it needs. Poetry has nothing to do with that. |
When I'm deploying the application I don't work with But in local development I'm using this simple single line to update the environment before doning
|
While working with one of my super clever coworkers this week, I happily learned how to work around this "issue" with very minimal overhead. I decided to try and share my education with the community by putting together a quick boilerplate repo. It is a really simple flask app setup with poetry and python-dotenv. (Be sure to check out the Justfile!) |
@Vozf My issue is that this feature was already implemented, and the only reason it's not implemented as of yet in Poetry itself is because @sdispater doesn't want it. |
If you want this feature for django, you can use django-environ. Otherwise you can also use direnv. EDIT: Oh and python-dotenv looks really nice. |
These, along with all of the other suggestions that I have seen here do not apply to the 50-80% of computer users who are on windows (https://en.wikipedia.org/wiki/Usage_share_of_operating_systems#Desktop_and_laptop_computers). From my research the only established library that makes loading .env painless and automatic for Windows users of Python is Pipenv. |
It's interesting to see that one of the oldest open issues out there (#208) also relates to using environment variables. Also interesting to note is that this issue (#337) is one of the most commented open issues. Moreover, this issue has been raised more than once (#3324). There's also a PR handy (#339). Clearly the community is in favour of manipulating custom environment variables using poetry. @sdispater Is there a way where you'd be willing to reconsider this? |
Ok, so I converted my dev team last year to use poetry. One of our biggest python project depends on cx_Oracle. We happen to work with MacOS Catalina 10.15.7 and lately it is not enough any more to just do a poetry install. Before poetry install we need to issue So it is not any more just for the design of the developed application itself which lacks this functionality (which is arguably not the scope of poetry), but the very freaking reason we use poetry: one-stop-shop for installing dependencies. This is now rendered incomplete. It is not any more a one-stop-shop. |
@kottalovag I got frustrated with the lack of dotenv support also but also wanted a better task runner features like poethepoet supports. So I made a wrapper that includes these features: https://pypi.org/project/trashy-poetry/ It's a drop in replacement, just use |
I've developed a Rust application that reads .env file. See https://github.com/askoretskiy/readenv |
As you may already know, a plugin system for Poetry is currently being developed (see #3733) which will make it possible to build a plugin that will be in charge of loading There is a naive example of what it could look like here: https://github.com/python-poetry/poetry/blob/plugin-system-revamped/docs/docs/plugins.md#event-handler So I am hopeful that, once this lands in the |
@sdispater how close is the plugin system? Weeks,months, year? |
@pizzapanther A first alpha release with the plugin system should be available by the end of March, if all goes well. Note that this is just a target but not a promise. |
I've enhanced @miguendes' script to load |
For the linux users out there
|
I've thrown together a plugin based on the naive example: mpeteuil/poetry-dotenv-plugin (also on PyPi).
Poetry 1.2.0, which supports plugins, has been released as of August 31, 2022. |
I can't read my .env file, even with python-dotenv. Works fine when I pass the I have pretty much the same setup in a Flask app using regular venv, never had any problem, so I imagine it may have something to do with the way poetry implements venv? Any other ideas? |
for running django application with gunicorn in your wsgi.py remember to use loadenv
|
@sdispater I've used Poetry since I found it months ago and only have had good things to say. Thank you, you are a hero. But I thought to put my Google Cloud variables in a .env file because there are dev/qa/prod clouds. I've usually use What would be the most clean solution here? |
@ZetiMente If you're on Poetry >= 1.2.0a1, then you can use |
Probably the simplest solution with almost zero effort is this:
|
I'm just here to read the comments and this popcorn IS MAKING ME THIRSTY! 🍿😂 Thanks for the plugin, @mpeteuil. |
It might be nice to mention that .env file should be present in the top directory of the package for it to be loaded. I didn't notice that in https://python-poetry.org/docs/managing-environments/ or in |
I had a different location for .env for the package. But for the purpose of poetry to facilitate poetry run, I created a .env in top directory as a link to the original .env file |
@seekingThat The reason you were unable to find any documentation about |
This dude said "I don't understand what virtual environments have to do with environment variables." |
Thank you very much! Appreciate your clarification |
Note that poethepoet supports .env files since a while back: https://github.com/nat-n/poethepoet#environment-variables |
If you are using |
The downside of plugins (and various scripts to load Note also that in the latest poetry 1.2, the command to install the plugin is $ poetry self add poetry-dotenv-plugin |
Issue
First off I really like poetry- I'd actually only recently migrated over to pipenv but I already see the advantages of poetry and would ideally like to use it full time.
I primarily work with Django and other web frameworks that can require a large amount of configuration which- when following 12 factor app practices- involves lots of environment variables that necessitates the use of a
.env
file.Is there a possibility of adding in
.env
file support, specifically for poetry'srun
command so that the resulting command runs both in the virtualenv and with the environment as configured in the.env
file?The text was updated successfully, but these errors were encountered: