blob: 45abd59d18688d8996501339e3a53700c9197c4a [file] [log] [blame]
Vinay Sajipc4618e32012-07-10 08:21:07 +01001Creation of :ref:`virtual environments <venv-def>` is done by executing the
Brett Cannon15552c32016-07-08 10:46:21 -07002command ``venv``::
Vinay Sajipc4618e32012-07-10 08:21:07 +01003
Brett Cannon15552c32016-07-08 10:46:21 -07004 python3 -m venv /path/to/new/virtual/environment
Vinay Sajipc4618e32012-07-10 08:21:07 +01005
6Running this command creates the target directory (creating any parent
7directories that don't exist already) and places a ``pyvenv.cfg`` file in it
Brett Cannon15552c32016-07-08 10:46:21 -07008with a ``home`` key pointing to the Python installation from which the command
9was run. It also creates a ``bin`` (or ``Scripts`` on Windows) subdirectory
Vinay Sajipc4618e32012-07-10 08:21:07 +010010containing a copy of the ``python`` binary (or binaries, in the case of
11Windows). It also creates an (initially empty) ``lib/pythonX.Y/site-packages``
TROUVERIE Joachime8eb9722018-02-18 17:52:36 +010012subdirectory (on Windows, this is ``Lib\site-packages``). If an existing
13directory is specified, it will be re-used.
Vinay Sajipc4618e32012-07-10 08:21:07 +010014
Brett Cannon15552c32016-07-08 10:46:21 -070015.. deprecated:: 3.6
16 ``pyvenv`` was the recommended tool for creating virtual environments for
17 Python 3.3 and 3.4, and is `deprecated in Python 3.6
18 <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_.
19
20.. versionchanged:: 3.5
21 The use of ``venv`` is now recommended for creating virtual environments.
22
Vinay Sajipc4618e32012-07-10 08:21:07 +010023.. highlight:: none
24
Brett Cannon15552c32016-07-08 10:46:21 -070025On Windows, invoke the ``venv`` command as follows::
Vinay Sajipc4618e32012-07-10 08:21:07 +010026
Brett Cannon15552c32016-07-08 10:46:21 -070027 c:\>c:\Python35\python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010028
Brett Cannon15552c32016-07-08 10:46:21 -070029Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for
30your :ref:`Python installation <using-on-windows>`::
Vinay Sajipc4618e32012-07-10 08:21:07 +010031
cocoatomod609b0c2017-10-27 13:42:11 +090032 c:\>python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010033
34The command, if run with ``-h``, will show the available options::
35
Berker Peksaga9744ae2016-01-30 12:17:10 +020036 usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
37 [--upgrade] [--without-pip]
38 ENV_DIR [ENV_DIR ...]
Vinay Sajipc4618e32012-07-10 08:21:07 +010039
40 Creates virtual Python environments in one or more target directories.
41
42 positional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070043 ENV_DIR A directory to create the environment in.
Vinay Sajipc4618e32012-07-10 08:21:07 +010044
45 optional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070046 -h, --help show this help message and exit
47 --system-site-packages
48 Give the virtual environment access to the system
49 site-packages dir.
50 --symlinks Try to use symlinks rather than copies, when symlinks
51 are not the default for the platform.
52 --copies Try to use copies rather than symlinks, even when
53 symlinks are the default for the platform.
54 --clear Delete the contents of the environment directory if it
55 already exists, before environment creation.
56 --upgrade Upgrade the environment directory to use this version
57 of Python, assuming Python has been upgraded in-place.
58 --without-pip Skips installing or upgrading pip in the virtual
59 environment (pip is bootstrapped by default)
Nick Coghlan8fbdb092013-11-23 00:30:34 +100060
Brett Cannon15552c32016-07-08 10:46:21 -070061 Once an environment has been created, you may wish to activate it, e.g. by
62 sourcing an activate script in its bin directory.
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000063
Nick Coghlan8fbdb092013-11-23 00:30:34 +100064.. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070065 Installs pip by default, added the ``--without-pip`` and ``--copies``
66 options
Vinay Sajipc4618e32012-07-10 08:21:07 +010067
Vinay Sajip71e72962015-01-23 19:35:12 +000068.. versionchanged:: 3.4
69 In earlier versions, if the target directory already existed, an error was
TROUVERIE Joachime8eb9722018-02-18 17:52:36 +010070 raised, unless the ``--clear`` or ``--upgrade`` option was provided.
Vinay Sajipc4618e32012-07-10 08:21:07 +010071
72The created ``pyvenv.cfg`` file also includes the
73``include-system-site-packages`` key, set to ``true`` if ``venv`` is
74run with the ``--system-site-packages`` option, ``false`` otherwise.
75
Nick Coghlan8fbdb092013-11-23 00:30:34 +100076Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
77invoked to bootstrap ``pip`` into the virtual environment.
78
Brett Cannon15552c32016-07-08 10:46:21 -070079Multiple paths can be given to ``venv``, in which case an identical virtual
80environment will be created, according to the given options, at each provided
81path.
Vinay Sajipc4618e32012-07-10 08:21:07 +010082
Brett Cannon15552c32016-07-08 10:46:21 -070083Once a virtual environment has been created, it can be "activated" using a
84script in the virtual environment's binary directory. The invocation of the
Julien Palardd936a8f2018-11-21 09:40:05 +010085script is platform-specific (`<venv>` must be replaced by the path of the
86directory containing the virtual environment):
Vinay Sajipc4618e32012-07-10 08:21:07 +010087
Andrew Svetlov65e9c572012-10-04 21:48:58 +030088+-------------+-----------------+-----------------------------------------+
89| Platform | Shell | Command to activate virtual environment |
90+=============+=================+=========================================+
91| Posix | bash/zsh | $ source <venv>/bin/activate |
92+-------------+-----------------+-----------------------------------------+
93| | fish | $ . <venv>/bin/activate.fish |
94+-------------+-----------------+-----------------------------------------+
95| | csh/tcsh | $ source <venv>/bin/activate.csh |
96+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +030097| Windows | cmd.exe | C:\\> <venv>\\Scripts\\activate.bat |
Andrew Svetlov65e9c572012-10-04 21:48:58 +030098+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +030099| | PowerShell | PS C:\\> <venv>\\Scripts\\Activate.ps1 |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300100+-------------+-----------------+-----------------------------------------+
Vinay Sajipc4618e32012-07-10 08:21:07 +0100101
102You don't specifically *need* to activate an environment; activation just
Brett Cannon15552c32016-07-08 10:46:21 -0700103prepends the virtual environment's binary directory to your path, so that
104"python" invokes the virtual environment's Python interpreter and you can run
105installed scripts without having to use their full path. However, all scripts
106installed in a virtual environment should be runnable without activating it,
107and run with the virtual environment's Python automatically.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100108
Brett Cannon15552c32016-07-08 10:46:21 -0700109You can deactivate a virtual environment by typing "deactivate" in your shell.
110The exact mechanism is platform-specific: for example, the Bash activation
111script defines a "deactivate" function, whereas on Windows there are separate
112scripts called ``deactivate.bat`` and ``Deactivate.ps1`` which are installed
113when the virtual environment is created.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100114
R David Murray575fb312013-12-25 23:21:03 -0500115.. versionadded:: 3.4
116 ``fish`` and ``csh`` activation scripts.