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.



A black background with blue squares

AI-generated content may be incorrect.



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

  1. Create all the parent directories within which the virtual environment is going to exist, or the project directory
  2. Open Windows PowerShell and navigate to the directory where the virtual environment is going to be installed

cd </path/to/project/directory>

 

  1. Identify the path to the desired Python version installation that the virtual environment should run.  This is usually located in your machine’s Programs directory, and the <py_path> should be to a python.exe file
  2. Create the virtual environment in the project directory with the command below:

<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

  1. Open Windows PowerShell
  2. Navigate into the project directory where the virtual environment exists (DO NOT navigate into the virtual environment)
  3. Execute the following command in the PowerShell termrinal to activate the virtual environment:

./<virtual_environment_name>/Scripts/activate

 

  1. Confirm that the virtual environment is active by checking the path in the PowerShell terminal:

$ENV:Path

 

  1. Confirm that the virtual environment is using the correct version of Python, as indicated by the <py_path> when the virtual environment was created

python -v

 

  1. View all packages currently installed in a virtual environment from the PowerShell with the following command:

pip list

 

  1. The virtual environment is now activated, and PowerShell is running Python within the virtual environment that is isolated from the base

 

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.

  1. Execute the following command in the PowerShell terminal to activate the virtual environment:

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:

pip freeze

 

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:

pip freeze > requirements.txt

 

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:

  1. Open Windows PowerShell
  2. Navigate into the project directory
  3. Activate the virtual environment
  4. Execute the following command from the PowerShell command window:

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