Python
Virtual Environments
Python virtual
environments contain Python projects and their respective packages and dependencies.
Assuming that Python is installed on the machine, a virtual environment can be
created as it its own directory on top of an existing Python installation,
known as the virtual environment’s base.
Each virtual
environment will have its own binary copy of the indicated Python version,
which will run when the virtual environment is active. Furthermore, each
virtual environment will have its own collection of 3rd party libraries and
dependencies that the project can access.
When a virtual
environment is implemented, a project’s packages and dependencies are directly
installed in the environment and remain isolated from the base. This
allows a project to run Python and install packages without conflicting with
other existing packages or environments that exist on the system.
Separating
projects from other projects by virtual environment allows for versioning
variations among Python projects. This happens when different projects
are started at different points in time. In other words, packages’
versions can vary depending on when it was installed.
NOTE: A virtual environment’s
configurations cannot be changed after it has been created
Create a
Virtual Environment for Windows
cd </path/to/project/directory> |
<py_path> -m venv
</path/to/project/directory>/<virtual_environment_name> |
The
venv module from the indicated <py_path> Python installation will created
the virtual environment titled <virtual_environment_name>. Assuming that
the current working directory is where the virtual environment should be
created, the syntax can be simplified to:
<python_version> -m -venv
<virtual_environment_name> NOTE: The virtual environment will run the
Python version indicated in <py_path> |
Activate
the Virtual Environment
./<virtual_environment_name>/Scripts/activate |
$ENV:Path |
python -v |
pip list |
NOTE: A virtual environment will only be
active from a window that has it active. Multiple windows can have the same
virtual environment active, but not activating a virtual environment from the
window where a project is run uses the base environment.
Deactivate
the Virtual Environment
After work in a
given project is complete, it is good practice to exit the virtual environment.
This is done by deactivating the session in the window where it was made
active.
deactivate |
Requirements
File
A use case that
underscores the benefit of using a virtual environment is collaborative
development. The virtual environment will contain an entire project and
all its dependencies. However, sharing code with other developers
encourages all developers to use the same versions to improve compatibility.
One way to
document dependencies’ versions is to create a requirements file, which
indicates packages are their versions. This documentation allows
developers to know exactly what installations are necessary to run a project
without compatibility issues.
Creating a
Requirements File
Requirements
files are usually formatted as a text file titled ‘requirements.txt’. A
requirements file lists all the packages installed in an environment and their
versions with the following format:
<package_name>==<major_versioning>.<minor_versioning>.<patch_versioning>
The packages
and their versions as they're installed can be accessed from the PowerShell
terminal with the following command:
This file
exists in the project folder alongside the virtual environment. It can be
created and built manually in a .txt file following the format above.
Alternatively, a requirements file can be generated programmatically from the
PowerShell terminal with the following syntax:
Alternatively,
the pipreqs module scans a project's files import and
automatically builds a requirements file:
pip install pipreqs |
pipreqs </path/to/project/directory> |
Implementing
a Requirements File
A requirements
file’s contents can be installed in a virtual environment with the following
steps:
pip install -r requirements.txt |
The packages
and defined versions listed in the requirements files are now installed in the virtual
environment, and the project can access them when the virtual environment is
active.
Maintaining
Requirements
Check for
outdated packages with the following command in the PowerShell terminal:
pip list --outdated |
Manually
upgrade a specific package with the following command in the PowerShell
terminal:
pip install -U <package_name> |
Upgrade all
packages indicated in a requirements file with the following command in the
PowerShell terminal:
pip install -U -r requirements.txt |
Check for
missing dependencies with the following command in the PowerShell terminal:
python -m pip check |