Configuring a Python virtualenv in Debian
Last updated: Nov 5, 2022
Python virtual environments are a good idea. Naturally, I had a few problems getting mine to work properly. I found that my globally installed libraries were visible the activated venv. The whole point of a venv is to isolate libraries. The globally installed libraries that are not also explicitly installed in the venv should not be usable.
I use Debian and the bash shell.
So how did I fix what I found is a common problem?
Summary:
# blank the PYTHONPATH environment variable export PYTHONPATH= virtualenv my_venv ./my_venv/bin/source/activate
use python3, not python as a REPL or the interpreter in e.g. VSCode
use pip install, not python -m pip install
I have this line in my .bashrc file:
PYTHONPATH="${PYTHONPATH}:~/.local/lib/python3.7/site-packages/"
This means that when I create my venv, this path gets added to the PYTHONPATH environment variable in the venv. This allows the venv to see the packages in ~/.local/lib/python3.7/site-packages/. Which we do not want.
To prevent this, blank the PYTHONPATH before creating the venv by typing:
export PYTHONPATH=
We can check on what the PYTHONPATH environment variable is by entering:
echo $PYTHONPATH
This should now be blank.
Go ahead and create a new venv by typing e.g.
virtualenv my_venv
This is where things continued to be a little…. non-standard. I should be able to use:
python -m pip freeze
To see only the packages that I install in the venv. But I see all the global packages.
Using only:
pip
shows me what I should see. I would like to know why.
Similarly, I should get a REPL with access to only the packages installed in the venv using:
python
but typing this starts a REPL that allows me to import globally installed packages.
To get around this, use:
python3
Now things behave as they should. I cannot import a package that is globally installed but not installed in the venv. Which is the correct behaviour for a venv. I use the path to the python3 executable in the my_venv/bin directory as the interpreter in e.g. VSCode when I want to use a venv.
I found I had several installations of pip in various locations. I deleted all of them except for the one in /usr/bin.
If you want to learn more about virtual environments, I recommend this series of short videos: https://calmcode.io/virtualenv/intro.html
The videos recommend using the command:
python -m pip
in the venv, which I found to be unreliable.
Instead I just use:
pip
Check that this is pointing to the correct executable by typing:
which pip