How to Create and Manage Virtual Environments in Python

Creating virtual environments for Python in Windows (and Unix)

Matteo
Python in Plain English

--

Photo by Shahadat Rahman on Unsplash

In this guide, I will show you how to install and use virtual environments for Python using the libraries Virtualenv and VirtualenvWrapper. I will focus mostly on the Windows versions, but I will also show the commands for Unix (whenever they differ).

Why virtual environments?

When you first started using Python, you have probably just installed all the libraries you needed in the main environment. At least, this is what I did: after all, it is the easiest way to have everything you may need always available!

This is, however, not a really good idea. Suppose, for example, that you are working on two projects. In the first one (let’s call it project A) you have used a library that requires NumPy version 1.20.1, and in the second one (project B) you need to install another library that requires NumPy version 1.21.0. Now you are facing an unsolvable problem: you cannot update NumPy without breaking project A, but if you don’t do it you cannot work on project B!

A solution to this problem is to use virtual environments. These are, like the name says, virtual installations of Python, each one with its own libraries downloaded. So to solve our problem, we may use two different virtual environments:

  • virtualEnvironmentA will have installed the libraries needed by project A (so NumPy version 1.20.1)
  • virtualEnvironmentB will have installed the libraries needed by project B (so NumPy version 1.21.0).

Installation

In this tutorial we will use two libraries for creating virtual environments:

  • virtualenv which is used to create the virtual environment
  • virtualenvwrapper-win which is used to manage the virtual environments more easily. This is the Windows version of the library vitualenvwrapper . If you are using Unix, I will show you how to install the latter instead.

To install virtualenv, simply run in the command line (both on Windows and Unix):

pip install virtualenv

After this, we need to install virtualenvwrapper. You don’t need it to create virtual environments, but I recommend you install it anyway since it will allow you to store all of them in the same folder. On Windows run:

pip install virtualenvwrapper-win

On Unix replace virtualenvwrapper-win with virtualenvwrapper .

Using Virtualenv

Now that we have installed virtualenv, let’s see how this can be used to create the virtual environment for a Python program. First of all, you need to go (using the command line) to the folder where your program will be located. Here we will create the new virtual environment by running this command:

virtualenv ENV_NAME

where ENV_NAME can be any name you want. For example, running:

virtualenv myFirstVenv

will create a folder called myFirstVenv which contains the virtual version of Python. To activate it, simply run:

.\ENV_NAME\Scripts\activate

or, on Unix:

source ENV_NAME\bin\activate

On the command line you will now see something like this:

Image by Author

which means that you are using the Python environment installed in myFirstVenv instead of using the system version.

To exit, simply write on the command line:

deactivate

Managing Multiple Environments

After some time, you may end up having many virtual environments installed all over your computer. By using virtualenvwrapper, you can install all the environments in a single folder and therefore you can manage them more easily.

The folder that virtualenvwrappper uses is stored in a system variable called WORKON_HOME and its default value is %USERPROFILE%\Envs . To check where this folder is, you can run echo %WORKON_HOME%:

>echo %WORKON_HOME%
C:\User\TheGeneralistProgrammer\Envs

If you want to change it, follow these instructions.

Windows

In the path variables (that you can find if you right-click on “This Pc”, then click “properties” -> “advanced system settings” -> “environment variables”) add a new user variable named WORKON_HOME (or modify it if it already exists) and set the value to the folder you want to use.

Unix

Run in the command line:

export WORKON_HOME=<path>

where <path> should be replaced with the path to the folder where you want to save the environments.

Then you can check (both on Unix and Windows) if the folder is correct by running again:

echo %WORKON_HOME%

Note: you may need to close and reopen the command line before the folder is updated.

Creating a new Virtual Environment

To create a new virtual environment using virtualenvwrapper, you should run this command:

mkvirtualenv ENV_NAME

This will create and activate the new virtual environment in the folder you set as WORKON_HOME. To exit, you simply run deactivate as we did with virtualenv.

The next thing to learn is how to activate an already existing environment. The advantage of this wrapper is that you can activate with the same command any of the multiple environments you have created. First, to see a full list of the environments that you have created, run workon . The output will be something like this:

>workonPass a name to activate one of the following virtualenvs:
====================================================================
secondVenv
thirdVenv
venv

Then to activate a specific environment you can run:

workon ENV_NAME

Note: To see a list of the environments together with the position of the folder they are stored in, run lsvirtualenv on the command line.

Removing a Virtual Environment

With virtualenvwrapper, it is also easy to remove an environment that you no longer need using the command rmvirtualenv ENV_NAME. For example, to remove thirdVenv I will run:

>rmvirtualenv thirdVenvDeleted D:\Desktop\VirtualEnvironmentsWrapper\thirdVenv

Installing libraries

When you create a new virtual environment, it will not contain any library, even if they are installed in the system version of Python. However, you can install them using pip (after activating the environment) as you would do for the default Python installation.

Wouldn’t it be better to install all the libraries you need as soon as you are creating the environment? Luckily there is a method to do just that! First, we need a .txt file (usually called requirements.txt ) where are written the names of the libraries to install together with the version that we want for each one. We can create it from another environment where the libraries are already installed, by using the command:

pip freeze > requirements.txt

This will create a file that contains something like

numpy==1.21.1
pandas==1.3.1
python-dateutil==2.8.2
pytz==2021.1
six==1.16.0

(alternatively, you may create it yourself with the libraries you want). Then to create a new environment with the libraries already installed, run:

mkvirtualenv -r requirements.txt ENV_NAME

Using a different Python Version

Until now, all the virtual environments we have created were using the same python version as the default one. However, if you have installed multiple versions of Python (such as 3.7 and 3.8) on your computer, you can choose which one you want to use for a virtual environment with the optional parameter -p (or --python ). You only need to know the path to the Python executable of the desired version:

mkvirtualenv -p PATH_TO_PYTHON ENV_NAME

Conclusion

These are the things you need to know in order to start using Virtual Environments in Python. Thank you for reading through to the end, I hope I have helped you!

More content at plainenglish.io

--

--

A student, with a passion for programming and in particular Machine Learning.