blob: 1ada83c07a67f15032a8db8a106677a69d434063 [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
mkkotf5107df2018-12-14 21:28:52 +010010containing a copy/symlink of the Python binary/binaries (as appropriate for the
11platform or arguments used at environment creation time). It also creates an
12(initially empty) ``lib/pythonX.Y/site-packages`` subdirectory
13(on Windows, this is ``Lib\site-packages``). If an existing
TROUVERIE Joachime8eb9722018-02-18 17:52:36 +010014directory is specified, it will be re-used.
Vinay Sajipc4618e32012-07-10 08:21:07 +010015
Brett Cannon15552c32016-07-08 10:46:21 -070016.. deprecated:: 3.6
17 ``pyvenv`` was the recommended tool for creating virtual environments for
18 Python 3.3 and 3.4, and is `deprecated in Python 3.6
19 <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_.
20
21.. versionchanged:: 3.5
22 The use of ``venv`` is now recommended for creating virtual environments.
23
Vinay Sajipc4618e32012-07-10 08:21:07 +010024.. highlight:: none
25
Brett Cannon15552c32016-07-08 10:46:21 -070026On Windows, invoke the ``venv`` command as follows::
Vinay Sajipc4618e32012-07-10 08:21:07 +010027
Brett Cannon15552c32016-07-08 10:46:21 -070028 c:\>c:\Python35\python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010029
Brett Cannon15552c32016-07-08 10:46:21 -070030Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for
31your :ref:`Python installation <using-on-windows>`::
Vinay Sajipc4618e32012-07-10 08:21:07 +010032
cocoatomod609b0c2017-10-27 13:42:11 +090033 c:\>python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010034
35The command, if run with ``-h``, will show the available options::
36
Berker Peksaga9744ae2016-01-30 12:17:10 +020037 usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
Holger Frey32088802019-02-22 12:05:20 +010038 [--upgrade] [--without-pip] [--prompt PROMPT]
Berker Peksaga9744ae2016-01-30 12:17:10 +020039 ENV_DIR [ENV_DIR ...]
Vinay Sajipc4618e32012-07-10 08:21:07 +010040
41 Creates virtual Python environments in one or more target directories.
42
43 positional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070044 ENV_DIR A directory to create the environment in.
Vinay Sajipc4618e32012-07-10 08:21:07 +010045
46 optional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070047 -h, --help show this help message and exit
48 --system-site-packages
49 Give the virtual environment access to the system
50 site-packages dir.
51 --symlinks Try to use symlinks rather than copies, when symlinks
52 are not the default for the platform.
53 --copies Try to use copies rather than symlinks, even when
54 symlinks are the default for the platform.
55 --clear Delete the contents of the environment directory if it
56 already exists, before environment creation.
57 --upgrade Upgrade the environment directory to use this version
58 of Python, assuming Python has been upgraded in-place.
59 --without-pip Skips installing or upgrading pip in the virtual
60 environment (pip is bootstrapped by default)
Holger Frey32088802019-02-22 12:05:20 +010061 --prompt PROMPT Provides an alternative prompt prefix for this
62 environment.
Nick Coghlan8fbdb092013-11-23 00:30:34 +100063
Brett Cannon15552c32016-07-08 10:46:21 -070064 Once an environment has been created, you may wish to activate it, e.g. by
65 sourcing an activate script in its bin directory.
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000066
Nick Coghlan8fbdb092013-11-23 00:30:34 +100067.. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070068 Installs pip by default, added the ``--without-pip`` and ``--copies``
69 options
Vinay Sajipc4618e32012-07-10 08:21:07 +010070
Vinay Sajip71e72962015-01-23 19:35:12 +000071.. versionchanged:: 3.4
72 In earlier versions, if the target directory already existed, an error was
TROUVERIE Joachime8eb9722018-02-18 17:52:36 +010073 raised, unless the ``--clear`` or ``--upgrade`` option was provided.
Vinay Sajipc4618e32012-07-10 08:21:07 +010074
Steve Dowera1f9a332019-01-30 13:49:14 -080075.. note::
76 While symlinks are supported on Windows, they are not recommended. Of
77 particular note is that double-clicking ``python.exe`` in File Explorer
78 will resolve the symlink eagerly and ignore the virtual environment.
79
Vinay Sajipc4618e32012-07-10 08:21:07 +010080The created ``pyvenv.cfg`` file also includes the
81``include-system-site-packages`` key, set to ``true`` if ``venv`` is
82run with the ``--system-site-packages`` option, ``false`` otherwise.
83
Nick Coghlan8fbdb092013-11-23 00:30:34 +100084Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
85invoked to bootstrap ``pip`` into the virtual environment.
86
Brett Cannon15552c32016-07-08 10:46:21 -070087Multiple paths can be given to ``venv``, in which case an identical virtual
88environment will be created, according to the given options, at each provided
89path.
Vinay Sajipc4618e32012-07-10 08:21:07 +010090
Brett Cannon15552c32016-07-08 10:46:21 -070091Once a virtual environment has been created, it can be "activated" using a
92script in the virtual environment's binary directory. The invocation of the
Julien Palardd936a8f2018-11-21 09:40:05 +010093script is platform-specific (`<venv>` must be replaced by the path of the
94directory containing the virtual environment):
Vinay Sajipc4618e32012-07-10 08:21:07 +010095
Andrew Svetlov65e9c572012-10-04 21:48:58 +030096+-------------+-----------------+-----------------------------------------+
97| Platform | Shell | Command to activate virtual environment |
98+=============+=================+=========================================+
99| Posix | bash/zsh | $ source <venv>/bin/activate |
100+-------------+-----------------+-----------------------------------------+
101| | fish | $ . <venv>/bin/activate.fish |
102+-------------+-----------------+-----------------------------------------+
103| | csh/tcsh | $ source <venv>/bin/activate.csh |
104+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +0300105| Windows | cmd.exe | C:\\> <venv>\\Scripts\\activate.bat |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300106+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +0300107| | PowerShell | PS C:\\> <venv>\\Scripts\\Activate.ps1 |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300108+-------------+-----------------+-----------------------------------------+
Vinay Sajipc4618e32012-07-10 08:21:07 +0100109
110You don't specifically *need* to activate an environment; activation just
Brett Cannon15552c32016-07-08 10:46:21 -0700111prepends the virtual environment's binary directory to your path, so that
112"python" invokes the virtual environment's Python interpreter and you can run
113installed scripts without having to use their full path. However, all scripts
114installed in a virtual environment should be runnable without activating it,
115and run with the virtual environment's Python automatically.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100116
Brett Cannon15552c32016-07-08 10:46:21 -0700117You can deactivate a virtual environment by typing "deactivate" in your shell.
118The exact mechanism is platform-specific: for example, the Bash activation
119script defines a "deactivate" function, whereas on Windows there are separate
120scripts called ``deactivate.bat`` and ``Deactivate.ps1`` which are installed
121when the virtual environment is created.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100122
R David Murray575fb312013-12-25 23:21:03 -0500123.. versionadded:: 3.4
124 ``fish`` and ``csh`` activation scripts.