4
* `Discussion list <http://groups.google.com/group/python-virtualenv/>`_
5
* `Bugs <https://github.com/pypa/virtualenv/issues/>`_
14
.. comment: split here
19
You can install virtualenv with ``pip install virtualenv``, or the `latest
20
development version <https://github.com/pypa/virtualenv/tarball/develop>`_
21
with ``pip install https://github.com/pypa/virtualenv/tarball/develop``.
23
You can also use ``easy_install``, or if you have no Python package manager
24
available at all, you can just grab the single file `virtualenv.py`_ and run
25
it with ``python virtualenv.py``.
27
.. _virtualenv.py: https://raw.github.com/pypa/virtualenv/master/virtualenv.py
32
``virtualenv`` is a tool to create isolated Python environments.
34
The basic problem being addressed is one of dependencies and versions,
35
and indirectly permissions. Imagine you have an application that
36
needs version 1 of LibFoo, but another application requires version
37
2. How can you use both these applications? If you install
38
everything into ``/usr/lib/python2.7/site-packages`` (or whatever your
39
platform's standard location is), it's easy to end up in a situation
40
where you unintentionally upgrade an application that shouldn't be
43
Or more generally, what if you want to install an application *and
44
leave it be*? If an application works, any change in its libraries or
45
the versions of those libraries can break the application.
47
Also, what if you can't install packages into the global
48
``site-packages`` directory? For instance, on a shared host.
50
In all these cases, ``virtualenv`` can help you. It creates an
51
environment that has its own installation directories, that doesn't
52
share libraries with other virtualenv environments (and optionally
53
doesn't access the globally installed libraries either).
60
$ python virtualenv.py ENV
62
If you install it you can also just do ``virtualenv ENV``.
64
This creates ``ENV/lib/pythonX.X/site-packages``, where any libraries you
65
install will go. It also creates ``ENV/bin/python``, which is a Python
66
interpreter that uses this environment. Anytime you use that interpreter
67
(including when a script has ``#!/path/to/ENV/bin/python`` in it) the libraries
68
in that environment will be used.
70
It also installs either `Setuptools
71
<http://peak.telecommunity.com/DevCenter/setuptools>`_ or `distribute
72
<http://pypi.python.org/pypi/distribute>`_ into the environment. To use
73
Distribute instead of setuptools, just call virtualenv like this::
75
$ python virtualenv.py --distribute ENV
77
You can also set the environment variable VIRTUALENV_DISTRIBUTE.
79
A new virtualenv also includes the `pip <http://pypi.python.org/pypi/pip>`_
80
installer, so you can use ``ENV/bin/pip`` to install additional packages into
87
In a newly created virtualenv there will be a ``bin/activate`` shell
88
script. For Windows systems, activation scripts are provided for CMD.exe
91
On Posix systems you can do::
95
This will change your ``$PATH`` so its first entry is the virtualenv's
96
``bin/`` directory. (You have to use ``source`` because it changes your
97
shell environment in-place.) This is all it does; it's purely a
98
convenience. If you directly run a script or the python interpreter
99
from the virtualenv's ``bin/`` directory (e.g. ``path/to/env/bin/pip``
100
or ``/path/to/env/bin/python script.py``) there's no need for
103
After activating an environment you can use the function ``deactivate`` to
104
undo the changes to your ``$PATH``.
106
The ``activate`` script will also modify your shell prompt to indicate
107
which environment is currently active. You can disable this behavior,
108
which can be useful if you have your own custom prompt that already
109
displays the active environment name. To do so, set the
110
``VIRTUAL_ENV_DISABLE_PROMPT`` environment variable to any non-empty
111
value before running the ``activate`` script.
113
On Windows you just do::
115
> \path\to\env\Scripts\activate
117
And type `deactivate` to undo the changes.
119
Based on your active shell (CMD.exe or Powershell.exe), Windows will use
120
either activate.bat or activate.ps1 (as appropriate) to activate the
121
virtual environment. If using Powershell, see the notes about code signing
126
If using Powershell, the ``activate`` script is subject to the
127
`execution policies`_ on the system. By default on Windows 7, the system's
128
excution policy is set to ``Restricted``, meaning no scripts like the
129
``activate`` script are allowed to be executed. But that can't stop us
130
from changing that slightly to allow it to be executed.
132
In order to use the script, you have to relax your system's execution
133
policy to ``AllSigned``, meaning all scripts on the system must be
134
digitally signed to be executed. Since the virtualenv activation
135
script is signed by one of the authors (Jannis Leidel) this level of
136
the execution policy suffices. As an administrator run::
138
PS C:\> Set-ExecutionPolicy AllSigned
140
Then you'll be asked to trust the signer, when executing the script.
141
You will be prompted with the following::
143
PS C:\> virtualenv .\foo
144
New python executable in C:\foo\Scripts\python.exe
145
Installing setuptools................done.
146
Installing pip...................done.
147
PS C:\> .\foo\scripts\activate
149
Do you want to run software from this untrusted publisher?
150
File C:\foo\scripts\activate.ps1 is published by E=jannis@leidel.info,
151
CN=Jannis Leidel, L=Berlin, S=Berlin, C=DE, Description=581796-Gh7xfJxkxQSIO4E0
152
and is not trusted on your system. Only run scripts from trusted publishers.
153
[V] Never run [D] Do not run [R] Run once [A] Always run [?] Help
157
If you select ``[A] Always Run``, the certificate will be added to the
158
Trusted Publishers of your user account, and will be trusted in this
159
user's context henceforth. If you select ``[R] Run Once``, the script will
160
be run, but you will be prometed on a subsequent invocation. Advanced users
161
can add the signer's certificate to the Trusted Publishers of the Computer
162
account to apply to all users (though this technique is out of scope of this
165
Alternatively, you may relax the system execution policy to allow running
166
of local scripts without verifying the code signature using the following::
168
PS C:\> Set-ExecutionPolicy RemoteSigned
170
Since the ``activate.ps1`` script is generated locally for each virtualenv,
171
it is not considered a remote script and can then be executed.
173
.. _`execution policies`: http://technet.microsoft.com/en-us/library/dd347641.aspx
175
The ``--system-site-packages`` Option
176
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
178
If you build with ``virtualenv --system-site-packages ENV``, your virtual
179
environment will inherit packages from ``/usr/lib/python2.7/site-packages``
180
(or wherever your global site-packages directory is).
182
This can be used if you have control over the global site-packages directory,
183
and you want to depend on the packages there. If you want isolation from the
184
global system, do not use this flag.
187
Environment variables and configuration files
188
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
190
virtualenv can not only be configured by passing command line options such as
191
``--distribute`` but also by two other means:
193
- Environment variables
195
Each command line option is automatically used to look for environment
196
variables with the name format ``VIRTUALENV_<UPPER_NAME>``. That means
197
the name of the command line options are capitalized and have dashes
198
(``'-'``) replaced with underscores (``'_'``).
200
For example, to automatically install Distribute instead of setuptools
201
you can also set an environment variable::
203
$ export VIRTUALENV_DISTRIBUTE=true
204
$ python virtualenv.py ENV
206
It's the same as passing the option to virtualenv directly::
208
$ python virtualenv.py --distribute ENV
210
This also works for appending command line options, like ``--find-links``.
211
Just leave an empty space between the passsed values, e.g.::
213
$ export VIRTUALENV_EXTRA_SEARCH_DIR="/path/to/dists /path/to/other/dists"
216
is the same as calling::
218
$ python virtualenv.py --extra-search-dir=/path/to/dists --extra-search-dir=/path/to/other/dists ENV
222
virtualenv also looks for a standard ini config file. On Unix and Mac OS X
223
that's ``$HOME/.virtualenv/virtualenv.ini`` and on Windows, it's
224
``%APPDATA%\virtualenv\virtualenv.ini``.
226
The names of the settings are derived from the long command line option,
227
e.g. the option ``--distribute`` would look like this::
232
Appending options like ``--extra-search-dir`` can be written on multiple
240
Please have a look at the output of ``virtualenv --help`` for a full list
241
of supported options.
246
Some paths within the virtualenv are slightly different on Windows: scripts and
247
executables on Windows go in ``ENV\Scripts\`` instead of ``ENV/bin/`` and
248
libraries go in ``ENV\Lib\`` rather than ``ENV/lib/``.
250
To create a virtualenv under a path with spaces in it on Windows, you'll need
251
the `win32api <http://sourceforge.net/projects/pywin32/>`_ library installed.
256
Beginning with virtualenv version 1.5 `PyPy <http://pypy.org>`_ is
257
supported. To use PyPy 1.4 or 1.4.1, you need a version of virtualenv >= 1.5.
258
To use PyPy 1.5, you need a version of virtualenv >= 1.6.1.
260
Creating Your Own Bootstrap Scripts
261
-----------------------------------
263
While this creates an environment, it doesn't put anything into the
264
environment. Developers may find it useful to distribute a script
265
that sets up a particular environment, for example a script that
266
installs a particular web application.
268
To create a script like this, call
269
``virtualenv.create_bootstrap_script(extra_text)``, and write the
270
result to your new bootstrapping script. Here's the documentation
273
Creates a bootstrap script, which is like this script but with
274
extend_parser, adjust_options, and after_install hooks.
276
This returns a string that (written to disk of course) can be used
277
as a bootstrap script with your own customizations. The script
278
will be the standard virtualenv.py script, with your extra text
279
added (your extra text should be Python code).
281
If you include these functions, they will be called:
283
``extend_parser(optparse_parser)``:
284
You can add or remove options from the parser here.
286
``adjust_options(options, args)``:
287
You can change options here, or change the args (if you accept
288
different kinds of arguments, be sure you modify ``args`` so it is
289
only ``[DEST_DIR]``).
291
``after_install(options, home_dir)``:
293
After everything is installed, this function is called. This
294
is probably the function you are most likely to use. An
297
def after_install(options, home_dir):
298
if sys.platform == 'win32':
302
subprocess.call([join(home_dir, bin, 'easy_install'),
304
subprocess.call([join(home_dir, bin, 'my-package-script'),
307
This example immediately installs a package, and runs a setup
308
script from that package.
313
Here's a more concrete example of how you could use this::
315
import virtualenv, textwrap
316
output = virtualenv.create_bootstrap_script(textwrap.dedent("""
317
import os, subprocess
318
def after_install(options, home_dir):
319
etc = join(home_dir, 'etc')
320
if not os.path.exists(etc):
322
subprocess.call([join(home_dir, 'bin', 'easy_install'),
324
subprocess.call([join(home_dir, 'bin', 'paster'),
325
'make-config', 'BlogApplication',
326
join(etc, 'blog.ini')])
327
subprocess.call([join(home_dir, 'bin', 'paster'),
328
'setup-app', join(etc, 'blog.ini')])
330
f = open('blog-bootstrap.py', 'w').write(output)
332
Another example is available `here
333
<https://github.com/socialplanning/fassembler/blob/master/fassembler/create-venv-script.py>`_.
336
Using Virtualenv without ``bin/python``
337
---------------------------------------
339
Sometimes you can't or don't want to use the Python interpreter
340
created by the virtualenv. For instance, in a `mod_python
341
<http://www.modpython.org/>`_ or `mod_wsgi <http://www.modwsgi.org/>`_
342
environment, there is only one interpreter.
344
Luckily, it's easy. You must use the custom Python interpreter to
345
*install* libraries. But to *use* libraries, you just have to be sure
346
the path is correct. A script is available to correct the path. You
347
can setup the environment like::
349
activate_this = '/path/to/env/bin/activate_this.py'
350
execfile(activate_this, dict(__file__=activate_this))
352
This will change ``sys.path`` and even change ``sys.prefix``, but also allow
353
you to use an existing interpreter. Items in your environment will show up
354
first on ``sys.path``, before global items. However, global items will
355
always be accessible (as if the ``--system-site-packages`` flag had been used
356
in creating the environment, whether it was or not). Also, this cannot undo
357
the activation of other environments, or modules that have been imported.
358
You shouldn't try to, for instance, activate an environment before a web
359
request; you should activate *one* environment as early as possible, and not
360
do it again in that process.
362
Making Environments Relocatable
363
-------------------------------
365
Note: this option is somewhat experimental, and there are probably
366
caveats that have not yet been identified. Also this does not
367
currently work on Windows.
369
Normally environments are tied to a specific path. That means that
370
you cannot move an environment around or copy it to another computer.
371
You can fix up an environment to make it relocatable with the
374
$ virtualenv --relocatable ENV
376
This will make some of the files created by setuptools or distribute
377
use relative paths, and will change all the scripts to use ``activate_this.py``
378
instead of using the location of the Python interpreter to select the
381
**Note:** you must run this after you've installed *any* packages into
382
the environment. If you make an environment relocatable, then
383
install a new package, you must run ``virtualenv --relocatable``
386
Also, this **does not make your packages cross-platform**. You can
387
move the directory around, but it can only be used on other similar
388
computers. Some known environmental differences that can cause
389
incompatibilities: a different version of Python, when one platform
390
uses UCS2 for its internal unicode representation and another uses
391
UCS4 (a compile-time option), obvious platform changes like Windows
392
vs. Linux, or Intel vs. ARM, and if you have libraries that bind to C
393
libraries on the system, if those C libraries are located somewhere
394
different (either different versions, or a different filesystem
397
If you use this flag to create an environment, currently, the
398
``--system-site-packages`` option will be implied.
400
The ``--extra-search-dir`` option
401
---------------------------------
403
When it creates a new environment, virtualenv installs either
404
setuptools or distribute, and pip. In normal operation, the latest
405
releases of these packages are fetched from the `Python Package Index
406
<http://pypi.python.org>`_ (PyPI). In some circumstances, this
407
behavior may not be wanted, for example if you are using virtualenv
408
during a deployment and do not want to depend on Internet access and
411
As an alternative, you can provide your own versions of setuptools,
412
distribute and/or pip on the filesystem, and tell virtualenv to use
413
those distributions instead of downloading them from the Internet. To
414
use this feature, pass one or more ``--extra-search-dir`` options to
415
virtualenv like this::
417
$ virtualenv --extra-search-dir=/path/to/distributions ENV
419
The ``/path/to/distributions`` path should point to a directory that
420
contains setuptools, distribute and/or pip distributions. Setuptools
421
distributions must be ``.egg`` files; distribute and pip distributions
422
should be `.tar.gz` source distributions.
424
Virtualenv will still download these packages if no satisfactory local
425
distributions are found.
427
If you are really concerned about virtualenv fetching these packages
428
from the Internet and want to ensure that it never will, you can also
429
provide an option ``--never-download`` like so::
431
$ virtualenv --extra-search-dir=/path/to/distributions --never-download ENV
433
If this option is provided, virtualenv will never try to download
434
setuptools/distribute or pip. Instead, it will exit with status code 1
435
if it fails to find local distributions for any of these required
436
packages. The local distribution lookup is done in this order and the
439
#. The current directory.
440
#. The directory where virtualenv.py is located.
441
#. A ``virtualenv_support`` directory relative to the directory where
442
virtualenv.py is located.
443
#. If the file being executed is not named virtualenv.py (i.e. is a boot
444
script), a ``virtualenv_support`` directory relative to wherever
445
virtualenv.py is actually installed.
447
Compare & Contrast with Alternatives
448
------------------------------------
450
There are several alternatives that create isolated environments:
452
* ``workingenv`` (which I do not suggest you use anymore) is the
453
predecessor to this library. It used the main Python interpreter,
454
but relied on setting ``$PYTHONPATH`` to activate the environment.
455
This causes problems when running Python scripts that aren't part of
456
the environment (e.g., a globally installed ``hg`` or ``bzr``). It
457
also conflicted a lot with Setuptools.
460
<http://peak.telecommunity.com/DevCenter/EasyInstall#creating-a-virtual-python>`_
461
is also a predecessor to this library. It uses only symlinks, so it
462
couldn't work on Windows. It also symlinks over the *entire*
463
standard library and global ``site-packages``. As a result, it
464
won't see new additions to the global ``site-packages``.
466
This script only symlinks a small portion of the standard library
467
into the environment, and so on Windows it is feasible to simply
468
copy these files over. Also, it creates a new/empty
469
``site-packages`` and also adds the global ``site-packages`` to the
470
path, so updates are tracked separately. This script also installs
471
Setuptools automatically, saving a step and avoiding the need for
474
* `zc.buildout <http://pypi.python.org/pypi/zc.buildout>`_ doesn't
475
create an isolated Python environment in the same style, but
476
achieves similar results through a declarative config file that sets
477
up scripts with very particular packages. As a declarative system,
478
it is somewhat easier to repeat and manage, but more difficult to
479
experiment with. ``zc.buildout`` includes the ability to setup
480
non-Python systems (e.g., a database server or an Apache instance).
482
I *strongly* recommend anyone doing application development or
483
deployment use one of these tools.
488
Refer to the `contributing to pip`_ documentation - it applies equally to
489
virtualenv, except that virtualenv issues should filed on the `virtualenv
492
Virtualenv's release schedule is tied to pip's -- each time there's a new pip
493
release, there will be a new virtualenv release that bundles the new version of
496
.. _contributing to pip: http://www.pip-installer.org/en/latest/contributing.html
497
.. _virtualenv repo: https://github.com/pypa/virtualenv/
502
Virtualenv's test suite is small and not yet at all comprehensive, but we aim
505
The easy way to run tests (handles test dependencies automatically)::
507
$ python setup.py test
509
If you want to run only a selection of the tests, you'll need to run them
510
directly with nose instead. Create a virtualenv, and install required
513
$ pip install nose mock
519
Or select just a single test file to run::
521
$ nosetests tests.test_virtualenv
524
Other Documentation and Links
525
-----------------------------
527
* James Gardner has written a tutorial on using `virtualenv with
529
<http://wiki.pylonshq.com/display/pylonscookbook/Using+a+Virtualenv+Sandbox>`_.
532
<http://blog.ianbicking.org/2007/10/10/workingenv-is-dead-long-live-virtualenv/>`_.
534
* Doug Hellmann wrote a description of his `command-line work flow
535
using virtualenv (virtualenvwrapper)
536
<http://www.doughellmann.com/articles/CompletelyDifferent-2008-05-virtualenvwrapper/index.html>`_
537
including some handy scripts to make working with multiple
538
environments easier. He also wrote `an example of using virtualenv
540
<http://www.doughellmann.com/articles/CompletelyDifferent-2008-02-ipython-and-virtualenv/index.html>`_.
542
* Chris Perkins created a `showmedo video including virtualenv
543
<http://showmedo.com/videos/video?name=2910000&fromSeriesID=291>`_.
545
* `Using virtualenv with mod_wsgi
546
<http://code.google.com/p/modwsgi/wiki/VirtualEnvironments>`_.
548
* `virtualenv commands
549
<https://github.com/thisismedium/virtualenv-commands>`_ for some more
550
workflow-related tools around virtualenv.
555
``virtualenv`` is a successor to `workingenv
556
<http://cheeseshop.python.org/pypi/workingenv.py>`_, and an extension
558
<http://peak.telecommunity.com/DevCenter/EasyInstall#creating-a-virtual-python>`_.
560
It was written by Ian Bicking, sponsored by the `Open Planning
561
Project <http://openplans.org>`_ and is now maintained by a
562
`group of developers <https://github.com/pypa/virtualenv/raw/master/AUTHORS.txt>`_.
563
It is licensed under an
564
`MIT-style permissive license <https://github.com/pypa/virtualenv/raw/master/LICENSE.txt>`_.