blob: af30cbb55980714d2cff8aa0871c5f041800d2b2 [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
Miss Islington (bot)0cba1212019-06-28 12:36:09 -07009was run (a common name for the target directory is ``.venv``). It also creates
10a ``bin`` (or ``Scripts`` on Windows) subdirectory containing a copy/symlink
11of the Python binary/binaries (as appropriate for the platform or arguments
12used at environment creation time). It also creates an (initially empty)
13``lib/pythonX.Y/site-packages`` subdirectory (on Windows, this is
14``Lib\site-packages``). If an existing directory is specified, it will be
15re-used.
Vinay Sajipc4618e32012-07-10 08:21:07 +010016
Brett Cannon15552c32016-07-08 10:46:21 -070017.. deprecated:: 3.6
18 ``pyvenv`` was the recommended tool for creating virtual environments for
19 Python 3.3 and 3.4, and is `deprecated in Python 3.6
20 <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_.
21
22.. versionchanged:: 3.5
23 The use of ``venv`` is now recommended for creating virtual environments.
24
Vinay Sajipc4618e32012-07-10 08:21:07 +010025.. highlight:: none
26
Brett Cannon15552c32016-07-08 10:46:21 -070027On Windows, invoke the ``venv`` command as follows::
Vinay Sajipc4618e32012-07-10 08:21:07 +010028
Brett Cannon15552c32016-07-08 10:46:21 -070029 c:\>c:\Python35\python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010030
Brett Cannon15552c32016-07-08 10:46:21 -070031Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for
32your :ref:`Python installation <using-on-windows>`::
Vinay Sajipc4618e32012-07-10 08:21:07 +010033
cocoatomod609b0c2017-10-27 13:42:11 +090034 c:\>python -m venv c:\path\to\myenv
Vinay Sajipc4618e32012-07-10 08:21:07 +010035
36The command, if run with ``-h``, will show the available options::
37
Berker Peksaga9744ae2016-01-30 12:17:10 +020038 usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
Holger Frey32088802019-02-22 12:05:20 +010039 [--upgrade] [--without-pip] [--prompt PROMPT]
Berker Peksaga9744ae2016-01-30 12:17:10 +020040 ENV_DIR [ENV_DIR ...]
Vinay Sajipc4618e32012-07-10 08:21:07 +010041
42 Creates virtual Python environments in one or more target directories.
43
44 positional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070045 ENV_DIR A directory to create the environment in.
Vinay Sajipc4618e32012-07-10 08:21:07 +010046
47 optional arguments:
Brett Cannon15552c32016-07-08 10:46:21 -070048 -h, --help show this help message and exit
49 --system-site-packages
50 Give the virtual environment access to the system
51 site-packages dir.
52 --symlinks Try to use symlinks rather than copies, when symlinks
53 are not the default for the platform.
54 --copies Try to use copies rather than symlinks, even when
55 symlinks are the default for the platform.
56 --clear Delete the contents of the environment directory if it
57 already exists, before environment creation.
58 --upgrade Upgrade the environment directory to use this version
59 of Python, assuming Python has been upgraded in-place.
60 --without-pip Skips installing or upgrading pip in the virtual
61 environment (pip is bootstrapped by default)
Holger Frey32088802019-02-22 12:05:20 +010062 --prompt PROMPT Provides an alternative prompt prefix for this
63 environment.
Nick Coghlan8fbdb092013-11-23 00:30:34 +100064
Brett Cannon15552c32016-07-08 10:46:21 -070065 Once an environment has been created, you may wish to activate it, e.g. by
66 sourcing an activate script in its bin directory.
Vinay Sajipc7e34fb2015-02-07 10:52:02 +000067
Nick Coghlan8fbdb092013-11-23 00:30:34 +100068.. versionchanged:: 3.4
Larry Hastings3732ed22014-03-15 21:13:56 -070069 Installs pip by default, added the ``--without-pip`` and ``--copies``
70 options
Vinay Sajipc4618e32012-07-10 08:21:07 +010071
Vinay Sajip71e72962015-01-23 19:35:12 +000072.. versionchanged:: 3.4
73 In earlier versions, if the target directory already existed, an error was
TROUVERIE Joachime8eb9722018-02-18 17:52:36 +010074 raised, unless the ``--clear`` or ``--upgrade`` option was provided.
Vinay Sajipc4618e32012-07-10 08:21:07 +010075
Steve Dowera1f9a332019-01-30 13:49:14 -080076.. note::
77 While symlinks are supported on Windows, they are not recommended. Of
78 particular note is that double-clicking ``python.exe`` in File Explorer
79 will resolve the symlink eagerly and ignore the virtual environment.
80
Vinay Sajipc4618e32012-07-10 08:21:07 +010081The created ``pyvenv.cfg`` file also includes the
82``include-system-site-packages`` key, set to ``true`` if ``venv`` is
83run with the ``--system-site-packages`` option, ``false`` otherwise.
84
Nick Coghlan8fbdb092013-11-23 00:30:34 +100085Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be
86invoked to bootstrap ``pip`` into the virtual environment.
87
Brett Cannon15552c32016-07-08 10:46:21 -070088Multiple paths can be given to ``venv``, in which case an identical virtual
89environment will be created, according to the given options, at each provided
90path.
Vinay Sajipc4618e32012-07-10 08:21:07 +010091
Brett Cannon15552c32016-07-08 10:46:21 -070092Once a virtual environment has been created, it can be "activated" using a
93script in the virtual environment's binary directory. The invocation of the
Julien Palardd936a8f2018-11-21 09:40:05 +010094script is platform-specific (`<venv>` must be replaced by the path of the
95directory containing the virtual environment):
Vinay Sajipc4618e32012-07-10 08:21:07 +010096
Andrew Svetlov65e9c572012-10-04 21:48:58 +030097+-------------+-----------------+-----------------------------------------+
98| Platform | Shell | Command to activate virtual environment |
99+=============+=================+=========================================+
Miss Islington (bot)0cba1212019-06-28 12:36:09 -0700100| POSIX | bash/zsh | $ source <venv>/bin/activate |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300101+-------------+-----------------+-----------------------------------------+
102| | fish | $ . <venv>/bin/activate.fish |
103+-------------+-----------------+-----------------------------------------+
104| | csh/tcsh | $ source <venv>/bin/activate.csh |
105+-------------+-----------------+-----------------------------------------+
Miss Islington (bot)0cba1212019-06-28 12:36:09 -0700106| | PowerShell Core | $ <venv>/bin/Activate.ps1 |
107+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +0300108| Windows | cmd.exe | C:\\> <venv>\\Scripts\\activate.bat |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300109+-------------+-----------------+-----------------------------------------+
Berker Peksag6803f352016-06-27 13:10:47 +0300110| | PowerShell | PS C:\\> <venv>\\Scripts\\Activate.ps1 |
Andrew Svetlov65e9c572012-10-04 21:48:58 +0300111+-------------+-----------------+-----------------------------------------+
Vinay Sajipc4618e32012-07-10 08:21:07 +0100112
113You don't specifically *need* to activate an environment; activation just
Brett Cannon15552c32016-07-08 10:46:21 -0700114prepends the virtual environment's binary directory to your path, so that
115"python" invokes the virtual environment's Python interpreter and you can run
116installed scripts without having to use their full path. However, all scripts
117installed in a virtual environment should be runnable without activating it,
118and run with the virtual environment's Python automatically.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100119
Brett Cannon15552c32016-07-08 10:46:21 -0700120You can deactivate a virtual environment by typing "deactivate" in your shell.
121The exact mechanism is platform-specific: for example, the Bash activation
122script defines a "deactivate" function, whereas on Windows there are separate
123scripts called ``deactivate.bat`` and ``Deactivate.ps1`` which are installed
124when the virtual environment is created.
Vinay Sajipc4618e32012-07-10 08:21:07 +0100125
R David Murray575fb312013-12-25 23:21:03 -0500126.. versionadded:: 3.4
127 ``fish`` and ``csh`` activation scripts.
Miss Islington (bot)0cba1212019-06-28 12:36:09 -0700128
129.. versionadded:: 3.8
130 PowerShell activation scripts installed under POSIX for PowerShell Core
131 support.