3.2.7. Python virtual environments#
A virtual environment is a self-contained directory tree containing Python and some additional packages.
These are some advantages of using virtual environments.
No administrator privileges#
You do not need administrator privileges to create a virtual environment or install packages in an environment.
Multiple virtual environments can be created with each containing their own sets of packages.
These are isolated and packages in an environment can be installed, updated or removed without affecting other environments.
Creating a virtual environment#
First, using The quick and easy method, search and
python3-venv package. This includes the
venv module necessary for creating virtual environments.
You can create a virtual environment in any directory where you have write privileges, for example, your home directory.
To demonstrate, I will create a virtual environment called
py3env in my home directory.
python3 -m venv py3env
If successful, you will find a directory named
in the current directory. No messages will be displayed.
python3 is the command to run the Python 3
interpreter. Its complete path is
venv is the Python module to create virtual
-m option runs the
venv module as a script.
Before you can use a virtual environment, you will need to activate it.
Activating a virtual environment#
You will need to activate a virtual environment before you
can start using it. To do so, use the
with the path to the virtual environment’s
For example, to activate
py3env created in the
previous step, use:
Your shell prompt will now change to indicate that
the virtual environment is now active.
(py3env) label at the beginning of the
You can now start using this virtual environment.
Before you start installing packages…
These build tools are necessary for building and installing packages from PyPI and other sources.
Installing them will ensure that additional packages will build and install without errors.
Installing Python build tools#
pip3 install to install or upgrade the
pip3 install -U pip setuptools wheel
-U option of
pip3 install, will upgrade
listed packages, if newer versions are available.
Collecting pip Downloading pip-21.0.1-py3-none-any.whl (1.5 MB) ... Installing collected packages: pip, setuptools, wheel ... Successfully installed pip-21.0.1 setuptools-54.2.0 wheel-0.36.2
Deactivating a virtual environment#
To exit a virtual environment, use the command:
Your shell prompt will change to its original appearance: