blob: 02bcbeeb7f2bc228253338f5dcd7bcd0d30713c4 [file] [log] [blame]
Vinay Sajipc4618e32012-07-10 08:21:07 +01001Creation of :ref:`virtual environments <venv-def>` is done by executing the
2``pyvenv`` script::
3
4 pyvenv /path/to/new/virtual/environment
5
6Running this command creates the target directory (creating any parent
7directories that don't exist already) and places a ``pyvenv.cfg`` file in it
8with a ``home`` key pointing to the Python installation the command was run
9from. It also creates a ``bin`` (or ``Scripts`` on Windows) subdirectory
10containing a copy of the ``python`` binary (or binaries, in the case of
11Windows). It also creates an (initially empty) ``lib/pythonX.Y/site-packages``
12subdirectory (on Windows, this is ``Lib\site-packages``).
13
Larry Hastings3732ed22014-03-15 21:13:56 -070014.. seealso::
15
16 `Python Packaging User Guide: Creating and using virtual environments
Georg Brandl525d3552014-10-29 10:26:56 +010017 <https://packaging.python.org/en/latest/installing.html#virtual-environments>`__
Larry Hastings3732ed22014-03-15 21:13:56 -070018
Vinay Sajipc4618e32012-07-10 08:21:07 +010019.. highlight:: none
20
21On Windows, you may have to invoke the ``pyvenv`` script as follows, if you
22don't have the relevant PATH and PATHEXT settings::
23
Vinay Sajipd0050902015-02-07 10:57:36 +000024 c:\Temp>c:\Python35\python c:\Python35\Tools\Scripts\pyvenv.py myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010025
26or equivalently::
27
Vinay Sajipd0050902015-02-07 10:57:36 +000028 c:\Temp>c:\Python35\python -m venv myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010029
30The command, if run with ``-h``, will show the available options::
31
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000032 usage: venv [-h] [--system-site-packages] [--symlinks] [--clear]
33 [--upgrade] [--without-pip] ENV_DIR [ENV_DIR ...]
Vinay Sajipc4618e32012-07-10 08:21:07 +010034
35 Creates virtual Python environments in one or more target directories.
36
37 positional arguments:
38 ENV_DIR A directory to create the environment in.
39
40 optional arguments:
41 -h, --help show this help message and exit
42 --system-site-packages Give access to the global site-packages dir to the
43 virtual environment.
44 --symlinks Try to use symlinks rather than copies, when symlinks
45 are not the default for the platform.
Larry Hastings3732ed22014-03-15 21:13:56 -070046 --copies Try to use copies rather than symlinks, even when
47 symlinks are the default for the platform.
Vinay Sajipc4618e32012-07-10 08:21:07 +010048 --clear Delete the environment directory if it already exists.
49 If not specified and the directory exists, an error is
50 raised.
51 --upgrade Upgrade the environment directory to use this version
52 of Python, assuming Python has been upgraded in-place.
Nick Coghlan8fbdb092013-11-23 00:30:34 +100053 --without-pip Skips installing or upgrading pip in the virtual
54 environment (pip is bootstrapped by default)
55
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000056Depending on how the ``venv`` functionality has been invoked, the usage message
57may vary slightly, e.g. referencing ``pyvenv`` rather than ``venv``.
58
Nick Coghlan8fbdb092013-11-23 00:30:34 +100059.. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070060 Installs pip by default, added the ``--without-pip`` and ``--copies``
61 options
Vinay Sajipc4618e32012-07-10 08:21:07 +010062
Vinay Sajip71e72962015-01-23 19:35:12 +000063.. versionchanged:: 3.4
64 In earlier versions, if the target directory already existed, an error was
65 raised, unless the ``--clear`` or ``--upgrade`` option was provided. Now,
66 if an existing directory is specified, its contents are removed and
67 the directory is processed as if it had been newly created.
Vinay Sajipc4618e32012-07-10 08:21:07 +010068
69The created ``pyvenv.cfg`` file also includes the
70``include-system-site-packages`` key, set to ``true`` if ``venv`` is
71run with the ``--system-site-packages`` option, ``false`` otherwise.
72
Nick Coghlan8fbdb092013-11-23 00:30:34 +100073Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
74invoked to bootstrap ``pip`` into the virtual environment.
75
Vinay Sajipc4618e32012-07-10 08:21:07 +010076Multiple paths can be given to ``pyvenv``, in which case an identical
77virtualenv will be created, according to the given options, at each
78provided path.
79
80Once a venv has been created, it can be "activated" using a script in the
Andrew Svetlov65e9c572012-10-04 21:48:58 +030081venv's binary directory. The invocation of the script is platform-specific:
Vinay Sajipc4618e32012-07-10 08:21:07 +010082
Andrew Svetlov65e9c572012-10-04 21:48:58 +030083+-------------+-----------------+-----------------------------------------+
84| Platform | Shell | Command to activate virtual environment |
85+=============+=================+=========================================+
86| Posix | bash/zsh | $ source <venv>/bin/activate |
87+-------------+-----------------+-----------------------------------------+
88| | fish | $ . <venv>/bin/activate.fish |
89+-------------+-----------------+-----------------------------------------+
90| | csh/tcsh | $ source <venv>/bin/activate.csh |
91+-------------+-----------------+-----------------------------------------+
92| Windows | cmd.exe | C:\> <venv>/Scripts/activate.bat |
93+-------------+-----------------+-----------------------------------------+
94| | PowerShell | PS C:\> <venv>/Scripts/Activate.ps1 |
95+-------------+-----------------+-----------------------------------------+
Vinay Sajipc4618e32012-07-10 08:21:07 +010096
97You don't specifically *need* to activate an environment; activation just
98prepends the venv's binary directory to your path, so that "python" invokes the
99venv's Python interpreter and you can run installed scripts without having to
100use their full path. However, all scripts installed in a venv should be
101runnable without activating it, and run with the venv's Python automatically.
102
103You can deactivate a venv by typing "deactivate" in your shell. The exact
104mechanism is platform-specific: for example, the Bash activation script defines
105a "deactivate" function, whereas on Windows there are separate scripts called
106``deactivate.bat`` and ``Deactivate.ps1`` which are installed when the venv is
107created.
108
R David Murray575fb312013-12-25 23:21:03 -0500109.. versionadded:: 3.4
110 ``fish`` and ``csh`` activation scripts.