Python virtual environments¶
The following is a quick survival guide for disambiguating Python virtual environments, the tools used to create them and terms such as:
- Installation of a distribution
- Virtual environment
There exists several Python distributions:
- CPython (official one downloadable from python.org)
Each distribution provides a Python interpreter that can execute Python
programs (i.e. a distribution provides the
Each distribution comes in multiple versions that's able to interpret the Python programming language - e.g Python 3.8, 3.9 etc.
Installation of a distribution¶
You can have one or more Python distributions installed at the computer at the same time. E.g. you can have CPython 3.7, 3.8, 3.9. Often, your operating system also already ships a default Python installation.
What's important to understand is that each installation of a distribution
provides an isolated Python environment. Technically this translates down
to having multiple
python commands in different paths (e.g.
Pay attention to symlinks and aliases, e.g.
python might be an alias
Python distribution tools¶
The most prime example of a Python distribution tool is
pip, which you use
to install Python packages from the Python Package Index (PyPI).
pip is always tied to a specific installation of a Python
distribution. So when you call
pip install ..., it will install the Python
package into that specific installation of a distribution.
Thus in addition to having multiple
python commands, you can have multiple
pip commands that belongs to a distribution.
A Python virtual environment is created from a distribution installation. It isolates the Python packages and commands installed.
For instance you have two Python programs A and B. Python program A depends on version 1 of package X, and program B depends on version 2 of the same package X. Both version 1 and version 2 cannot be installed at the same time in a single Python distribution.
Virtual environments allows you to have a virtual environment for program A and another for program B each with different version installed of package X.
This means that a virtual environment has it's own
commands that are enhanced aliases to the distributions
pyenvis a tool to manage and install multiple Python distributions.
virtualenvare tools to create virtual environments for a specific installation of a distribution.
virtualenv-wrapperare tools used to manage
aptare package manages that can be used to install e.g. multiple Python distributions.
pipenvon your local machine or in containers to create and manage a virtual environment.
All of this can be further complicated if you run container images or virtual machines