PyCharm has support for virtual environment.
But there are two problems:
- the virtual environment is not activated in the terminal (it is only in the python console)
- it doesn't support virtualenvwrapper and so doesn't run the
This gist is the solution I found to resolve these two problems.
The solution explained
To make this work, the simplest solution I found was to create a file
.venv in a directory with the name of the virtualenv to auto-activate inside.
Then, when you will be in this directory, or any sub-directory, by invoking the commands explained below, the virtualenv will be auto-activated.
This works with PyCharm because when it calls
bash for the terminal or
python for the python-console, it uses the PyCharm project directory as current working directory/
The solution is based on calling bash with a rc-file that will find the
.venv file, read it, and activate the virtualenv based on the name just read (This rc-file will also check for a
VENV environment variable, or will be able to find the venv if the current directory is a subdirectory of a venv directory)
For bash it's something like
/bin/bash --rcfile /path/to/rcfile
For python it's the same, we just tell bash to run the python command:
/bin/bash --rcfile /path/to/rcfile --ci python [args for python]
As the virtualenv will be set by the rcfile, we are sure that the
python will be the one of the virtualenv.
This gist include a script to call for python instead of this whole line.
Start by copying the others file in this Gist:
Then make the
python files executable:
chmod +x ~/bin/python-autovenv
Configure your project
Start by creating a
.venv file in your PyCharm project directory (ie the same directory that holds the
.idea directory), and simple add the name of the virtualenv on the first line (nothing else on the first line, it's important)
Now we can configure PyCharm.
What's magic here is that we can do this "by default" for all our PyCharm python project, because it relies on this
.venv file and if not found, it will simply work without activating any virtualenv.
Now we can adapt a pycharm project for which you want your virtualenv activated everywhere, with its
postactivate script executed.
We'll have to change two default settings in PyCharm:
Changing the python console interpreter
By default, PyCharm use the python binary from your virtualenv. It allows to work with default virtualenv, with the correct path for modules, but it's not enough to execute the
File > Default settings then go to
Default project > Project interpreter.
Project interpreter line, click on the icon on the right, then
Add local, and enter
~/bin/python-autovenv then click
Now you'll want to select this
python-autovenv interpreter for the default python console. Open
File > Default settings then
Build, Execution, Deployment > Console > Python console and in the
Python interpreter field, select the one with
python-autovenv in it.
When done, you may want to check for each of your python project if it's the correct one, and set it the same way if not (idem for
Changing the shell to use in the terminal
By default, PyCharm use
/bin/bash to open a new terminal. This doesn't activate your virtualenv. You man want it to be activated and your
postactivate file execute.
File > Default settings then go to
Tools > Terminal.
Shell path field, enter
/bin/bash --rcfile ~/.bashrc-autovenv
Then in PyCharm, you can open a new terminal, it will work as expected.
You may want to restart PyCharm one these settings are changed.
It may not be the better way to do this, I'm not at all a bash ninja, and there is a little overhead for python as instead of running python directly, we launch a script in bash that will launch another instance of bash. But It works for me, and I'm happy.
This trick allows two little things not mentioned above:
- for a django project using environment variable in configurations, needed for the settings file to work, it now works and the PyCharm python console is now able to run it for django.
- you can use this outside of PyCharm, by calling
python-autovenvinstead of python and it will run in your virtualenv, assuming you have in the directory (or a parent one), a