-
-
Save markph0204/dd97d9d301483fcc2cf458f5e44ff25d to your computer and use it in GitHub Desktop.
# enable direnv for python | |
# this will enable the commandline support as well as support pycharm | |
# 1 install direnv (Homebrew / pip) | |
# 2 edit your .bashrc, .bash_profile or .bash_aliases | |
function venv-here { | |
# you could just use 'layout python' here for 2.7.x | |
echo "layout python3" > .envrc | |
echo "ln -s .direnv/\$(basename \$VIRTUAL_ENV)/ .env" >> .envrc | |
} | |
# sample | |
# cd to any path, then run... | |
venv-here | |
# prompted to run direnv allow | |
direnv allow | |
#done! |
I think it relies on this PyCharm plugin
see this article for more
On a side note, what I do is just run sed -n -e 's/export \(.*\)/\1/p' .envrc > .env
to put my .envrc contents into .env to use the plugin.
To be clear, this wouldn't allow you to use direnv-stdlib
, like path_add
?
Thanks for this gist. Finally got direnv with venv support and pycharm to work.
I received an error on direnv activate
with the above script:
ln: .env/python-venv-3.7.4: File exists
direnv: error exit status 1
so changed the symlink creation a bit to make it work all the time
echo "ln -sf .direnv/\$(basename \$VIRTUAL_ENV)/ .env" >> .envrc
Add this to the end of your .envrc
:
# to keep a .env file in sync (for Pycharm), keep this at the very end of the file for (head -2 to work)
sed -n -e 's/export \(.*\)/\1/p' .envrc | head -n -2 > .env
Then run direnv allow
. This will copy your vars from .envrc
to .env
except the last two lines.
Bonus: on Pycharm add a new test run configuration profile, example pytest
, set module name to tests
(or whatever your tests dir is), set interpreter to be Project Default
, change tab to EnvFile
, enable Enable EnvFile
, then add a new path with the +
sign, then .env file
then navigate to your project root, at the top enable viewing of hidden files (the square button), select your .env
file and press OK, then apply your changes and press OK on the configuration window. Run your tests -> Profit.
@ joaomcarlos there's actually even an easier solution, just place:
dotenv
it your .envrc
and it will automatically source .env
My biggest problem is that .env
offers subset of functionality of direnv. When I integrate direnv with Nix I can define not only an environment variables but all developer tooling. That means exact python version that I used during development, with all the python packages, and all system library dependencies my project needs.
Then all I need is to cd
into directory with the projects and suddenly I have everything I need for development installed.
It would be awesome if PyCharm could obtain setup that way.
Hi,
This gist has been invaluable. Thanks for putting it out there! :)
PyCharm detects & loads my venv, so all good there! :)
But, I have however, been confused by the symbolic link to .direnv
in echo "ln -sf .direnv/\$(basename \$VIRTUAL_ENV)/ .env" >> .envrc
. I get this message printed, direnv: .env at .env not found
, which is the result of runningdotenv .env
. Is it just a kludge to get PyCharm to detect and load venv's?
I'm using layout pipenv
so any venv's created are actually located in ~/.local/share/virtualenvs/
.
I don't find any references to the .direnv
file anywhere on my system and it's rather difficult tracking it down on the net too so it would be great if someone could offer an explanation please! :)
Just for reference:
function pipenv-venv {
echo "pipenv --python $(<.python-version)" > .envrc
echo "layout pipenv" >> .envrc
echo "ln -sf .direnv/\$(basename \$VIRTUAL_ENV)/ .env" >> .envrc
echo "dotenv .env" >> .envrc
}
Thanks again! I really appreciate it!
Is it just a kludge to get PyCharm to detect and load venv's?
Yes from what I recall -- .env was linked because that is what PyCharm use to only look for.
I don't find any references to the .direnv file anywhere on my system and it's rather difficult tracking it down on the net too so it would be great if someone could offer an explanation please! :)
I think you mean this? https://direnv.net
Thanks @markph0204 ! I really appreciate your reply. I didn't think you/anyone would get to it so quickly! 🥇 👍
I want to specifically understand why .direnv
is being used in this context:
echo "ln -s .direnv/\$(basename \$VIRTUAL_ENV)/ .env" >> .envrc
I looked all through https://direnv.net and only found references to .direnv
here in the layout python
and layout ruby
sections of direnv stdlib
.
So I think from that reading, that it looks like a $PWD/.direnv
file is only created for those layout types. And since I'm using pipenv
, it will not be created.
I did some testing and found that I didn't have to have anything but an empty .env
file in my project for PyCharm to detect the pipenv
venv.
So now the function can be written differently:
function pipenv-venv {
echo "pipenv --python $(<.python-version)" > .envrc
echo "layout pipenv" >> .envrc
echo "dotenv .env" >> .envrc
echo "pipenv update" >> .envrc
# PyCharm fix - an empty .env file
echo "" > .env
}
Thanks a lot!
Looking at DirEnv and Python with PyCharm right now -- trying to understand how this helps. I don't see anything in PyCharm docs that suggest it will automatically pick up environments with the name
.env
. Does it?