When working with Python, often we need to use different packages and modules. Some of them are part of Python’s Standard Library but many of them are not. On the other hand, we may need a specific version of a custom module for our app. Upgrading or downgrading to that version for a whole system is a risky idea because it can break other applications that are dependant on a specific version of a module.
Virtual Environments are here to help us. Starting from Python version 3.3, a module called
venv was added to the language spec, allowing users to set up a Python virtual environment more natively and supported by the Python development team.
See PEP 405.
Before version 3.3 this was only possible via installing 3rd-party packages, primarily Ian Bicking’s virtualenv. Although, these 3rd-party packages are still popular and in use, Python officially recommends the use of
venv for creating virtual environments since version 3.5.
First of all, if you are a Debian or Debian-based OS user 🔗, you need to make sure that you have the python3-venv package installed on your system:
🚀 ~ sudo apt install python3-venv
The creation of a typical virtual environment for Python looks like this:
🚀 ~ python3 -m venv /path/to/new/virtual/environment
Often, a Python virtual environment is installed in the current working directory and named
.venv. Some IDEs, like PyCharm, support an automatic setup of such virtual environments. Let’s create our virtual environment and name it
🚀 ~ python3 -m venv venv
After the virtual environment has been created, you need to activate it. To do that you have to run the following command:
🚀 ~ source venv/bin/activate
🚀 ~ . venv/bin/activate
Notice the change in your terminal, it now contains a mark
(venv) at the beginning.
(venv) 🚀 ~
This means that you are running inside a virtual environment. If you do
python3 -c "import site; print(site.PREFIXES)" here, you’ll see that python3 site-packages directory prefix points to your virtual environment and not the system’s default one. You can now start installing packages or specific versions of packages via
pip3 to your virtual environment. They will be isolated from the rest of the system.
To exit from a virtual environment, you need to run the following command inside that virtual environment:
(venv) 🚀 ~ deactivate
In some sense, a Python virtual environment created with
venv is similar to a Linux jail, except that in Linux jail you have completely self-contained, independent binaries while in Python virtual environment your python binary is just a symlink to the system’s default, typically located under /usr/bin/ directory. Also, in Linux jail, you run from within the jail and don’t have access to the outside system while in Python virtual environment all system commands and directories are still available and the real magic happens during the activation step when the program modifies the user’s $PATH variable and “overrides” the default paths/locations to python and pip binaries.
References; Statement Separator by Jakub Przywóski (2015)