Skip to main content

Virtual Environments

Virtual environments are used to isolate python packages for a python project. The following packages are available from the python distribution to create and manage virtual environments: venv for Python 3 or virtualenv for Python 2.

Notes

  • You should exclude your virtual environment directory from your version control system using .gitignore or similar.

  • Virtual environments are activated for just the terminal window you have open; they're not activated for your whole machine.

  • It's considered a best practice to use a separate virtual environment for each python project on your machine.

Using Python

This section covers creating and managing packages using the standard python distribution libraries.

Note: If you are using Python 3.3 or newer, the venv module is the preferred way to create and manage virtual environments. venv is included in the Python standard library and requires no additional installation.

References

Create Virtual Environment

Execute this command from your project directory to create a virtual environment with the name 'env'.

py -m venv env

or

python -m venv env

Activate Virtual Environment

Execute this command from your project directory to activate an existing virtual environment.

.\env\Scripts\activate

Windows Execuution Policy Errors

You may need to set an execution policy in order to run these scripts.

Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

Deactivate Virtual Environment

Execute this command from your project directory to deactivate an active virtual environment.

deactivate

Install Packages in Virtual Environment

Here's an example of installing packages within an active virtual environment.

See Python Docs - Installing Packages for examples

py -m pip install requests

or

py -m pip install "requests==2.18.4"

or

python -m pip install requests

List Installed Packages

Run this command to list the installed dependencies and their versions.

py -m pip list

or

python -m pip list

Requirements File

PIP supports defining package dependencies using a requirements file named requirements.txt.

Sample requirements.txt

requests==2.18.4
google-auth==1.1.0

Install from requirements.txt

py -m pip install -r requirements.txt

or

python -m pip install -r requirements.txt

Using Anaconda

This section covers creating and managing virtual environments using Anaconda.

Create Virtual Environments Using Anaconda

The virtual environment will be used to isolate packages that a script imports.

List packages for current virtual environment

conda list

Create a new virtual environment based on info in a YAML file

The environment.yaml file lists information such as the environment name and specific packages.

environment.yaml

name: some-env-name
channels:
- defaults
dependencies:
- pandas=1.5.3
- pillow=9.3.0
- python=3.11.3
conda env create -f environment.yaml

List the available environments

The active environment will have an asterisk (*) and the default environment is named 'base'.

conda env list

Activate the environment

Note that some-env-name is the environment name specified in the environment.yaml file above.

conda activate some-env-name

Deactivate the active virtual environment

Deactivate virtual environments once you are done with them.

conda deactivate