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.
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.
Install virtualenv and virtualenvwrapper with pip.
$ pip install virtualenv
For macOS and Linux:
$ pip install virtualenvwrapper
$ pip install virtualenvwrapper-win
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
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
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
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
To create a new virtual environment, use the mkvirtualenv command.
$ mkvirtualenv my-project (my-project) $
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 $
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
Now, select the virtual environment that you want to use:
$ workon 2048-game (2048-game) $
To remove an environment, use the rmvirtualenv command.
$ rmvirtualenv 2048-game Removing 2048-game...
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
That’s it! virtualenvwrapper allows you to easily manage your Python virtual environments, so that you can work on different projects without breaking things.