virtualenv is a tool used to create an isolated workspace for a Python application. It has various advantages such as the ability to install modules locally, export a working environment, and execute a Python program in that environment.
A virtual environment is a directory into which some binaries and shell scripts are installed. The binaries include python for executing scripts and pip for installing other modules within the environment. There are also shell scripts (one for bash, csh, and fish) to activate the environment. Essentially, a virtual environment mimics a full system install of Python and all of the desired modules without interfering with any system on which the application might run.
Python 3.3+ comes with a tool called pyvenv and an API called venv for extending the native implementation. For applications that require an older version of Python, virtualenv must be used.
- Python 3.3+:
- Python 3:
- Python 2:
All three tools use a similar workflow.
Use pyvenv or virtualenv to create the virtual environment within your project directory. Be sure to exclude the venv directory from version control--a copy of
pip freeze will be enough to rebuild it.
This tool is provided by(3.3+).
$ pyvenv venv
Use virtualenv for Python 3, available in.
$ virtualenv venv
And virtualenv2 for Python 2, available in.
$ virtualenv2 venv
Use one of the provided shell scripts to activate and deactivate the environment. This example assumes bash is used.
$ source venv/bin/activate (venv) $
Once inside the virtual environment, modules can be installed with pip and scripts can be run as normal.
To exit the virtual environment, run the function provided by
(venv) $ deactivate
The binary versions depend on which virtual environment tool was used. For instance, the python command used in the Python 2 example points to
bin/python2.7, while the one in the pyvenv example points to
One major difference between pyvenv and virtualenv is that the former uses the system's Python binary by default:
$ ls -l pyvenv/bin/python3.5 lrwxrwxrwx 1 foo foo 7 Jun 3 19:57 pyvenv/bin/python3.5 -> /usr/bin/python3
The virtualenv tool uses a separate Python binary in the environment directory:
$ ls -l venv3/bin/python3.5 lrwxrwxrwx 1 foo foo 7 Jun 3 19:58 venv3/bin/python3.5 -> python3
virtualenvwrapper allows more natural command line interaction with your virtual environemnts by exposing several useful commands to create, activate and remove virtual environments. This package is a wrapper for bothand .
Now add the following lines to your
$ export WORKON_HOME=~/.virtualenvs $ source /usr/bin/virtualenvwrapper.sh
If you are not using python3 by default (check the output of
python --version) you also need to add the following line to your
~/.bashrc prior sourcing the
virtualenvwrapper.sh script. The current version of the package only works with python3. It can create python2 virtual environments fine though.
Re-open your console and create the
$ mkdir $WORKON_HOME
The main information source on virtualenvwrapper usage (and extension capability) is Doug Hellmann's page.
Create the virtual environment:
$ mkvirtualenv -p /usr/bin/python2.7 my_env
Activate the virtual environment:
$ workon my_env
Install some package inside the virtual environment (say, Django):
(my_env) $ pip install django
After you have done your things, leave the virtual environment:
(my_env) $ deactivate