Virtualenv¶
Mailing list | Issues | Github | PyPI | User IRC: #pypa Dev IRC: #pypa-dev
Introduction¶
virtualenv
is a tool to create isolated Python environments. Since
Python 3.3, a subset of it has been integrated into the standard
library under the venv module.
Note though, that the venv
module does not offer all features of this
library (e.g. cannot create bootstrap scripts, cannot create virtual
environments for other python versions than the host python,
not relocatable, etc.). Tools in general as such still may prefer using
virtualenv for its ease of upgrading (via pip), unified handling of different
Python versions and some more advanced features.
The basic problem being addressed is one of dependencies and versions,
and indirectly permissions. Imagine you have an application that
needs version 1 of LibFoo, but another application requires version
2. How can you use both these applications? If you install
everything into /usr/lib/python2.7/site-packages
(or whatever your
platform’s standard location is), it’s easy to end up in a situation
where you unintentionally upgrade an application that shouldn’t be
upgraded.
Or more generally, what if you want to install an application and leave it be? If an application works, any change in its libraries or the versions of those libraries can break the application.
Also, what if you can’t install packages into the global
site-packages
directory? For instance, on a shared host.
In all these cases, virtualenv
can help you. It creates an
environment that has its own installation directories, that doesn’t
share libraries with other virtualenv environments (and optionally
doesn’t access the globally installed libraries either).
- Installation
- User Guide
- Reference Guide
- Development
- Release History
- Beyond v16.7.11
- v16.7.11 (2021-07-20)
- v16.7.10 (2020-02-24)
- v16.7.9 (2019-12-15)
- v16.7.8 (2019-11-22)
- v16.7.7 (2019-10-22)
- v16.7.6 (2019-10-16)
- v16.7.5 (2019-09-03)
- v16.7.4 (2019-08-23)
- v16.7.3 (2019-08-16)
- v16.7.2 (2019-07-26)
- v16.7.1 (2019-07-25)
- v16.7.0 (2019-07-23)
- v16.6.2 (2019-07-14)
- v16.6.1 (2019-06-16)
- v16.6.0 (2019-05-15)
- v16.5.0 (2019-04-24)
- v16.4.3 (2019-03-01)
- v16.4.1 (2019-02-22)
- v16.4.0 (2019-02-09)
- v16.3.0 (2019-01-25)
- v16.2.0 (2018-12-31)
- v16.1.0 (2018-10-31)
- v16.0.0 (2018-05-16)
- v15.2.0 (2018-03-21)
- v15.1.0 (2016-11-15)
- v15.0.3 (2016-08-05)
- v15.0.2 (2016-05-28)
- v15.0.1 (2016-03-17)
- v15.0.0 (2016-03-05)
- v14.0.6 (2016-02-07)
- v14.0.5 (2016-02-01)
- v14.0.4 (2016-01-31)
- v14.0.3 (2016-01-28)
- v14.0.2 (2016-01-28)
- v14.0.1 (2016-01-21)
- v14.0.0 (2016-01-19)
- v13.1.2 (2015-08-23)
- v13.1.1 (2015-08-20)
- v13.1.0 (2015-06-30)
- v13.0.3 (2015-06-01)
- v13.0.2 (2015-06-01)
- v13.0.1 (2015-05-22)
- v13.0.0 (2015-05-21)
- v12.1.1 (2015-04-07)
- v12.1.0 (2015-04-07)
- v12.0.7 (2015-02-04)
- v12.0.6 (2015-01-28)
- v12.0.5 (2015-01-03)
- v12.0.4 (2014-12-23)
- v12.0.3 (2014-12-23)
- v12.0.2 (2014-12-23)
- v12.0.1 (2014-12-22)
- v12.0 (2014-12-22)
- v1.11.6 (2014-05-16)
- v1.11.5 (2014-05-03)
- v1.11.4 (2014-02-21)
- v1.11.3 (2014-02-20)
- v1.11.2 (2014-01-26)
- v1.11.1 (2014-01-20)
- v1.11 (2014-01-02)
- v1.10.1 (2013-08-07)
- v1.10 (2013-07-23)
- v1.9.1 (2013-03-08)
- v1.9 (2013-03-07)
- v1.8.4 (2012-11-25)
- v1.8.3 (2012-11-21)
- v1.8.2 (2012-09-06)
- v1.8.1 (2012-09-03)
- v1.8 (2012-09-01)
- v1.7.2 (2012-06-22)
- v1.7.1.2 (2012-02-17)
- v1.7.1.1 (2012-02-16)
- v1.7.1 (2012-02-16)
- v1.7 (2011-11-30)
- v1.6.4 (2011-07-21)
- v1.6.3 (2011-07-16)
- v1.6.2 (2011-07-16)
- v1.6.1 (2011-04-30)
- v1.6
- v1.5.2
- v1.5.1
- v1.5
- v1.4.9
- v1.4.8
- v1.4.7
- v1.4.6
- v1.4.5
- v1.4.4
- v1.4.3
- v1.4.2
- v1.4.1
- v1.4
- v1.3.4
- v1.3.3
- v1.3.2
- v1.3.1
- v1.3
- v1.2
- v1.1.1
- v1.1
- v1.0
- v0.9.2
- v0.9.1
- v0.9
- v0.8.4
- v0.8.3
- v0.8.2
- v0.8.1
- v0.8
Other Documentation and Links¶
- Blog announcement of virtualenv.
- James Gardner has written a tutorial on using virtualenv with Pylons.
- Chris Perkins created a showmedo video including virtualenv.
- Doug Hellmann’s virtualenvwrapper is a useful set of scripts to make your workflow with many virtualenvs even easier. His initial blog post on it. He also wrote an example of using virtualenv to try IPython.
- Pew is another wrapper for virtualenv that makes use of a different activation technique.
- Using virtualenv with mod_wsgi.
- virtualenv commands for some more workflow-related tools around virtualenv.
- PyCon US 2011 talk: Reverse-engineering Ian Bicking’s brain: inside pip and virtualenv. By the end of the talk, you’ll have a good idea exactly how pip and virtualenv do their magic, and where to go looking in the source for particular behaviors or bug fixes.
Compare & Contrast with Alternatives¶
There are several alternatives that create isolated environments:
Python 3’s venv module is recommended for projects that no longer need to support Python 2 and want to create just simple environments for the host python.
workingenv
(which I do not suggest you use anymore) is the predecessor to this library. It used the main Python interpreter, but relied on setting$PYTHONPATH
to activate the environment. This causes problems when running Python scripts that aren’t part of the environment (e.g., a globally installedhg
orbzr
). It also conflicted a lot with Setuptools.virtual-python is also a predecessor to this library. It uses only symlinks, so it couldn’t work on Windows. It also symlinks over the entire standard library and global
site-packages
. As a result, it won’t see new additions to the globalsite-packages
.This script only symlinks a small portion of the standard library into the environment, and so on Windows it is feasible to simply copy these files over. Also, it creates a new/empty
site-packages
and also adds the globalsite-packages
to the path, so updates are tracked separately. This script also installs Setuptools automatically, saving a step and avoiding the need for network access.zc.buildout doesn’t create an isolated Python environment in the same style, but achieves similar results through a declarative config file that sets up scripts with very particular packages. As a declarative system, it is somewhat easier to repeat and manage, but more difficult to experiment with.
zc.buildout
includes the ability to setup non-Python systems (e.g., a database server or an Apache instance).
I strongly recommend anyone doing application development or deployment use one of these tools.