Python Environment Notes
Python virtual environments are isolated environments on your system, they are primarily used for testing and developing python projects, as well as safely bypassing this annoying error:
I used to just go the lazy route and do
sudo rm -f /usr/lib/python*/EXTERNALLY-MANAGEDBut this can lead to problems when other package managers. Since python is integrated with the operating system there are certain python packages that are installed on an operating system level. Say for example you run a
pip3 install -r requirements.txt
and in requirements.txt it installs a package from PyCrypto. This could possibly overwrite a system installation of PyCryptodome which is used by the operating system, which could break existing features or make them insecure.
It’s always safer to run the -r requirements.txt with — user or
pip3 install --user -r requirements.txt
But in general it’s better practice to use virtual environments. Knowing how to use them can safe lots of time in many situations.
The primary goal of virtual environments is to manage dependencies, versions, and packages without conflicts across different projects. Complex python projects often depend on different versions of external libraries, or even python itself. There are multiple virtual environment managers, such as Poetry, CONDA, and pipenv the primary and most popular manager is venv. Also venv is installed by default with python. It’s also a module so the syntax for starting a virtual environmnet is:
python -m venv <name>
Get Samuel McLamb’s stories in your inbox
Join Medium for free to get updates from this writer.
You can name it anything you want but the convention is to name it “env”
python3 -m venv env
We can see that it’s a directory. To activate this directory, on Debian Linux at least can be done with the following command:
source env/bin/activate
This command, unlike running activate directly, will run the command in the current context, allowing environment variables to be updated in the current shell session. Running activate directly will not modify any of the environment variables in your current shell.
To deactivate the python environment to go back to the original one type simply deactivate
and this brings us back to our original shell
Now when using pip we don’t encounter that error anymore:
We can also see using the which command that it’s installed in the local bin directory not the system bin directory.









