Python Virtual Environments in Windows

What is a Python Virtual Environment?

A Python virtual environment is an isolated Python environment that allows you to maintain separate dependencies for different Python projects. Setting up a Python virtual environments for each project ensures that it will have the requirements it needs without interference from other projects.

Why use a Python Virtual Environment?

When using Python, you might need to install packages and modules that aren't a part of the standard library. While doing a 'pip install –user some_package' will allow you to install Python packages in your home directory, you may run into issues later on with package dependencies. For example, if you are working on two different projects and Project 1 requires version 1.0 of a library whereas Project 2 requires version 2.0 of that same library, installing the dependency for Project 2 will likely break Project 1. An easy way around this is to create a separate virtual environment for each of your projects. The virtual environments are isolated from each other, allowing you to install dependencies for one project without worrying about breaking something in the other project.

Starting up the Anaconda prompt

Connect to the CLA WTS servers and select Start → All Programs  → Anaconda$version → Anaconda Prompt (where $version denotes the Anaconda Python version you want to use). This will open up a command window that is configured to use Python, conda, and other tools.

Creating a Python Virtual Environment

In the Anaconda Prompt window, you will notice that the prompt is preceded with (base). This indicates that you are in the default environment named "base." You don't want to install programs into the base environment. Instead, you will want to use the 'conda' command to create your own virtual environment to keep your projects isolated from each other. In this example, we will create a Python 3.6 virtual environment named 'Project1'.

conda create -n Project1 python=3.6

Using the Virtual Environment

To use the virtual environment, use the conda command to activate it.

conda activate Project1

After activating the virtual environment, the command prompt will be preceded by '(Project1)' instead of '(base)' to indicate that you are now in the Project1 virtual environment. Once you have activated the environment, you can install packages and run any Python scripts associated with the project.

Deactivating the Virtual Environment

When you are done working in your virtual environment, you will want to deactivate it using the 'deactivate' command. Your prompt will return to normal, indicating your virtual environment is no longer active and your PATH and shell variables have been returned to normal.

conda deactivate

Putting it all Together

conda create -n Project1 python=3.6
conda activate Project1
conda deactivate
conda create -n Project2 python=3.6
conda activate Project2
conda deactivate

Listing your Virtual Environments

To see a list of the Python virtual environments that you have created, you can use the 'conda env list' command. This command will give you the names as well as the filesystem paths for the location of your virtual environments.

conda env list

Note that in the list of environments, one of the environments will have an asterisk (*) by it. This is used to denote the environment that is currently active.

Listing Packages Installed in a Virtual Environment

After activating a virtual environment, if you want to see what packages are installed, you can use the 'conda list' command list the packages as well as their versions.

conda activate Project1
conda list

Installing Packages to a Virtual Environment

Virtual environments are created with a barebones site library when not linked to the base Anaconda installation. Unless your code only relies upon the Python standard library, you will want to use either the conda or pip package managers to install additional libraries.

Differences between conda and pip

The differences between conda and pip are often misunderstood as both package managers provide similar functionality. On one hand, pip is well-adopted in the Python community and the Python Package Index (PyPI) contains a vast array of packages maintained by the community. On the other hand, Conda is a general-purpose package manager, can help track dependencies for non-Python libraries, and is fully compatible with pip. In general, each package manager compliments the other and you are free to use whichever provides the package you wish to install.

Common Commands

Function Conda Pip

Search for a package in the default repo

conda search {package}

pip search {package}

Install a package

conda install {package}

pip install {package}

List installed packages

conda list

pip list

Upgrade a package

conda upgrade {package}

pip install --upgrade {package}

Remove a package

conda remove {package}

pip uninstall {package}

Deleting a Virtual Environment

When you are done with a project and have no further need for the associated Python virtual environment, you can delete it with the 'conda remove' command.

conda remove -n Project1 –all

Note: if you get an error that states “cannot remove current environment. deactivate and run conda remove again”, make sure that you have deactivated the environment. If the environment has been deactivated and you still get the same error, use the 'p' switch to the 'conda remove' command to specify the path to the virtual environment. (You can get the paths for all of your virtual environments with the 'conda env list' command.)

conda remove -p /path/to/virtual/env --all