Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 1 | Creation of :ref:`virtual environments <venv-def>` is done by executing the |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 2 | command ``venv``:: |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 3 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 4 | python3 -m venv /path/to/new/virtual/environment |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 5 | |
| 6 | Running this command creates the target directory (creating any parent |
| 7 | directories that don't exist already) and places a ``pyvenv.cfg`` file in it |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 8 | with a ``home`` key pointing to the Python installation from which the command |
Brett Cannon | f9f8e3c | 2019-06-28 12:14:31 -0700 | [diff] [blame] | 9 | was run (a common name for the target directory is ``.venv``). It also creates |
| 10 | a ``bin`` (or ``Scripts`` on Windows) subdirectory containing a copy/symlink |
| 11 | of the Python binary/binaries (as appropriate for the platform or arguments |
| 12 | used 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 |
| 15 | re-used. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 16 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 17 | .. 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 Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 25 | .. highlight:: none |
| 26 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 27 | On Windows, invoke the ``venv`` command as follows:: |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 28 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 29 | c:\>c:\Python35\python -m venv c:\path\to\myenv |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 30 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 31 | Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for |
| 32 | your :ref:`Python installation <using-on-windows>`:: |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 33 | |
cocoatomo | d609b0c | 2017-10-27 13:42:11 +0900 | [diff] [blame] | 34 | c:\>python -m venv c:\path\to\myenv |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 35 | |
| 36 | The command, if run with ``-h``, will show the available options:: |
| 37 | |
Berker Peksag | a9744ae | 2016-01-30 12:17:10 +0200 | [diff] [blame] | 38 | usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] |
Cooper Lees | 4acdbf1 | 2019-06-17 11:18:14 -0700 | [diff] [blame] | 39 | [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps] |
Berker Peksag | a9744ae | 2016-01-30 12:17:10 +0200 | [diff] [blame] | 40 | ENV_DIR [ENV_DIR ...] |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 41 | |
| 42 | Creates virtual Python environments in one or more target directories. |
| 43 | |
| 44 | positional arguments: |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 45 | ENV_DIR A directory to create the environment in. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 46 | |
| 47 | optional arguments: |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 48 | -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 Frey | 3208880 | 2019-02-22 12:05:20 +0100 | [diff] [blame] | 62 | --prompt PROMPT Provides an alternative prompt prefix for this |
| 63 | environment. |
Cooper Lees | 4acdbf1 | 2019-06-17 11:18:14 -0700 | [diff] [blame] | 64 | --upgrade-deps Upgrade core dependencies: pip setuptools to the |
| 65 | latest version in PyPI |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 66 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 67 | Once an environment has been created, you may wish to activate it, e.g. by |
| 68 | sourcing an activate script in its bin directory. |
Vinay Sajip | c7e34fb | 2015-02-07 10:52:02 +0000 | [diff] [blame] | 69 | |
johnthagen | 1264d04 | 2020-09-05 16:53:47 -0400 | [diff] [blame] | 70 | .. versionchanged:: 3.9 |
Cooper Lees | 4acdbf1 | 2019-06-17 11:18:14 -0700 | [diff] [blame] | 71 | Add ``--upgrade-deps`` option to upgrade pip + setuptools to the latest on PyPI |
| 72 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 73 | .. versionchanged:: 3.4 |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 74 | Installs pip by default, added the ``--without-pip`` and ``--copies`` |
| 75 | options |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 76 | |
Vinay Sajip | 71e7296 | 2015-01-23 19:35:12 +0000 | [diff] [blame] | 77 | .. versionchanged:: 3.4 |
| 78 | In earlier versions, if the target directory already existed, an error was |
TROUVERIE Joachim | e8eb972 | 2018-02-18 17:52:36 +0100 | [diff] [blame] | 79 | raised, unless the ``--clear`` or ``--upgrade`` option was provided. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 80 | |
Steve Dower | a1f9a33 | 2019-01-30 13:49:14 -0800 | [diff] [blame] | 81 | .. note:: |
| 82 | While symlinks are supported on Windows, they are not recommended. Of |
| 83 | particular note is that double-clicking ``python.exe`` in File Explorer |
| 84 | will resolve the symlink eagerly and ignore the virtual environment. |
| 85 | |
Derek Keeler | 45217af | 2020-04-02 12:00:21 -0700 | [diff] [blame] | 86 | .. note:: |
| 87 | On Microsoft Windows, it may be required to enable the ``Activate.ps1`` |
| 88 | script by setting the execution policy for the user. You can do this by |
| 89 | issuing the following PowerShell command: |
| 90 | |
| 91 | PS C:\> Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -Scope CurrentUser |
| 92 | |
| 93 | See `About Execution Policies |
Miro HronĨok | ef16958 | 2020-05-25 16:54:14 +0200 | [diff] [blame] | 94 | <https://go.microsoft.com/fwlink/?LinkID=135170>`_ |
Derek Keeler | 45217af | 2020-04-02 12:00:21 -0700 | [diff] [blame] | 95 | for more information. |
| 96 | |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 97 | The created ``pyvenv.cfg`` file also includes the |
| 98 | ``include-system-site-packages`` key, set to ``true`` if ``venv`` is |
| 99 | run with the ``--system-site-packages`` option, ``false`` otherwise. |
| 100 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 101 | Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be |
| 102 | invoked to bootstrap ``pip`` into the virtual environment. |
| 103 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 104 | Multiple paths can be given to ``venv``, in which case an identical virtual |
| 105 | environment will be created, according to the given options, at each provided |
| 106 | path. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 107 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 108 | Once a virtual environment has been created, it can be "activated" using a |
| 109 | script in the virtual environment's binary directory. The invocation of the |
Julien Palard | d936a8f | 2018-11-21 09:40:05 +0100 | [diff] [blame] | 110 | script is platform-specific (`<venv>` must be replaced by the path of the |
| 111 | directory containing the virtual environment): |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 112 | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 113 | +-------------+-----------------+-----------------------------------------+ |
| 114 | | Platform | Shell | Command to activate virtual environment | |
| 115 | +=============+=================+=========================================+ |
Brett Cannon | f9f8e3c | 2019-06-28 12:14:31 -0700 | [diff] [blame] | 116 | | POSIX | bash/zsh | $ source <venv>/bin/activate | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 117 | +-------------+-----------------+-----------------------------------------+ |
Brett Cannon | 84b1ff6 | 2019-11-22 23:32:27 -0800 | [diff] [blame] | 118 | | | fish | $ source <venv>/bin/activate.fish | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 119 | +-------------+-----------------+-----------------------------------------+ |
| 120 | | | csh/tcsh | $ source <venv>/bin/activate.csh | |
| 121 | +-------------+-----------------+-----------------------------------------+ |
Brett Cannon | f9f8e3c | 2019-06-28 12:14:31 -0700 | [diff] [blame] | 122 | | | PowerShell Core | $ <venv>/bin/Activate.ps1 | |
| 123 | +-------------+-----------------+-----------------------------------------+ |
Berker Peksag | 6803f35 | 2016-06-27 13:10:47 +0300 | [diff] [blame] | 124 | | Windows | cmd.exe | C:\\> <venv>\\Scripts\\activate.bat | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 125 | +-------------+-----------------+-----------------------------------------+ |
Berker Peksag | 6803f35 | 2016-06-27 13:10:47 +0300 | [diff] [blame] | 126 | | | PowerShell | PS C:\\> <venv>\\Scripts\\Activate.ps1 | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 127 | +-------------+-----------------+-----------------------------------------+ |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 128 | |
Andre Delfino | 3584d4b | 2020-09-01 03:07:29 -0300 | [diff] [blame] | 129 | When a virtual environment is active, the :envvar:`VIRTUAL_ENV` environment |
| 130 | variable is set to the path of the virtual environment. This can be used to |
| 131 | check if one is running inside a virtual environment. |
| 132 | |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 133 | You don't specifically *need* to activate an environment; activation just |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 134 | prepends the virtual environment's binary directory to your path, so that |
| 135 | "python" invokes the virtual environment's Python interpreter and you can run |
| 136 | installed scripts without having to use their full path. However, all scripts |
| 137 | installed in a virtual environment should be runnable without activating it, |
| 138 | and run with the virtual environment's Python automatically. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 139 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 140 | You can deactivate a virtual environment by typing "deactivate" in your shell. |
Derek Keeler | 91e4957 | 2019-07-26 14:57:11 -0700 | [diff] [blame] | 141 | The exact mechanism is platform-specific and is an internal implementation |
| 142 | detail (typically a script or shell function will be used). |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 143 | |
R David Murray | 575fb31 | 2013-12-25 23:21:03 -0500 | [diff] [blame] | 144 | .. versionadded:: 3.4 |
| 145 | ``fish`` and ``csh`` activation scripts. |
Brett Cannon | f9f8e3c | 2019-06-28 12:14:31 -0700 | [diff] [blame] | 146 | |
| 147 | .. versionadded:: 3.8 |
| 148 | PowerShell activation scripts installed under POSIX for PowerShell Core |
| 149 | support. |