DEV Community

Cover image for Managing Python Virtual Environments With virtualenvwrapper
Zhang Zeyu
Zhang Zeyu

Posted on

Managing Python Virtual Environments With virtualenvwrapper

What Is a Virtual Environment?

A virtual environment is an isolated Python environment. Working on a project in an isolated Python environment ensures that project dependencies are kept separate, and allows you to manage Python packages for different projects without breaking system tools or other projects.
For example, if both projects A and B depend on the same library, project C, but use different versions of it, Python would not be able to serve both versions of the library.

We can use virtual environments for projects A and B, and each virtual environment would be able to use their own version of project C without interfering with other virtual environments.

Why Use virtualenvwrapper?

At one point or another, any programmer would run into the problem of managing multiple virtual environments. virtualenvwrapper allows you to store all your virtual environments in one convenient location and provides methods to easily create, delete and switch between virtual environments. You can even specify different versions of Python for each virtual environment.

virtualenvwrapper is simply a set of extensions to virtualenv, making it easier to work with Python virtual environments.

Installing and Configuring virtualenvwrapper

Install virtualenv and virtualenvwrapper with pip.

$ pip install virtualenv
Enter fullscreen mode Exit fullscreen mode

For macOS and Linux:

$ pip install virtualenvwrapper
Enter fullscreen mode Exit fullscreen mode

For Windows:

$ pip install virtualenvwrapper-win
Enter fullscreen mode Exit fullscreen mode

Now, we need to add a few lines to the shell’s startup file. First, find the exact location of the installed virtualenvwrapper.sh script.

$ which virtualenvwrapper.sh
/usr/local/bin/virtualenvwrapper.sh
Enter fullscreen mode Exit fullscreen mode

Now, find your shell’s startup file. For Bash shell, it would be the ~/.bashrc file. Add the following lines into the file:

export WORKON_HOME=$HOME/.virtualenvs  
export PROJECT_HOME=$HOME/projects      
source /usr/local/bin/virtualenvwrapper.sh
Enter fullscreen mode Exit fullscreen mode

Note that you should change /usr/local/bin/virtualenvwrapper.sh to the path you got from $ which virtualenvwrapper.sh if they are different.

Reload the startup file:

$ source ~/.bashrc
Enter fullscreen mode Exit fullscreen mode

Check that it works; there should now be a directory at $WORKON_HOME that contains all your virtualenvwrapper files:

$ echo $WORKON_HOME
/Users/zhangzeyu/.virtualenvs
Enter fullscreen mode Exit fullscreen mode

Using virtualenvwrapper

To create a new virtual environment, use the mkvirtualenv command.

$ mkvirtualenv my-project
(my-project) $
Enter fullscreen mode Exit fullscreen mode

The new virtual environment is stored at the directory at $WORKON_HOME. This is a convenient location where all virtualenvwrapper environments are stored.

To stop using the environment, use the deactivate command.

(my-project) $ deactivate
$
Enter fullscreen mode Exit fullscreen mode

This is where the power of virtualenvwrapper comes in. To list all your virtual environments, use the workon function.

$ workon
my-project
my-other-project
i-have-many-projects
2048-game
Enter fullscreen mode Exit fullscreen mode

Now, select the virtual environment that you want to use:

$ workon 2048-game
(2048-game) $
Enter fullscreen mode Exit fullscreen mode

To remove an environment, use the rmvirtualenv command.

$ rmvirtualenv 2048-game
Removing 2048-game...
Enter fullscreen mode Exit fullscreen mode

Let’s say you want to use different versions of Python. mkvirtualenv has a -p parameter that allows you to select which version of Python to use.

$ mkvirtualenv python2-env -p python2
(python2-env) $ python -V
Python 2.7.17
Enter fullscreen mode Exit fullscreen mode

Conclusion

That’s it! virtualenvwrapper allows you to easily manage your Python virtual environments, so that you can work on different projects without breaking things.

Oldest comments (2)

Collapse
 
bassimsmile profile image
Bassim RAJI

From the day I was looking for something like that thank you 😁

Collapse
 
zeyu2001 profile image
Zhang Zeyu

Thanks for reading!