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 |
| 9 | was run. It also creates a ``bin`` (or ``Scripts`` on Windows) subdirectory |
mkkot | f5107df | 2018-12-14 21:28:52 +0100 | [diff] [blame] | 10 | containing a copy/symlink of the Python binary/binaries (as appropriate for the |
| 11 | platform 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 Joachim | e8eb972 | 2018-02-18 17:52:36 +0100 | [diff] [blame] | 14 | directory is specified, it will be re-used. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 15 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 16 | .. 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 Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 24 | .. highlight:: none |
| 25 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 26 | On Windows, invoke the ``venv`` command as follows:: |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 27 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 28 | c:\>c:\Python35\python -m venv c:\path\to\myenv |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 29 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 30 | Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for |
| 31 | your :ref:`Python installation <using-on-windows>`:: |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 32 | |
cocoatomo | d609b0c | 2017-10-27 13:42:11 +0900 | [diff] [blame] | 33 | c:\>python -m venv c:\path\to\myenv |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 34 | |
| 35 | The command, if run with ``-h``, will show the available options:: |
| 36 | |
Berker Peksag | a9744ae | 2016-01-30 12:17:10 +0200 | [diff] [blame] | 37 | usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] |
Holger Frey | 3208880 | 2019-02-22 12:05:20 +0100 | [diff] [blame] | 38 | [--upgrade] [--without-pip] [--prompt PROMPT] |
Berker Peksag | a9744ae | 2016-01-30 12:17:10 +0200 | [diff] [blame] | 39 | ENV_DIR [ENV_DIR ...] |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 40 | |
| 41 | Creates virtual Python environments in one or more target directories. |
| 42 | |
| 43 | positional arguments: |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 44 | ENV_DIR A directory to create the environment in. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 45 | |
| 46 | optional arguments: |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 47 | -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 Frey | 3208880 | 2019-02-22 12:05:20 +0100 | [diff] [blame] | 61 | --prompt PROMPT Provides an alternative prompt prefix for this |
| 62 | environment. |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 63 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 64 | 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 Sajip | c7e34fb | 2015-02-07 10:52:02 +0000 | [diff] [blame] | 66 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 67 | .. versionchanged:: 3.4 |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 68 | Installs pip by default, added the ``--without-pip`` and ``--copies`` |
| 69 | options |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 70 | |
Vinay Sajip | 71e7296 | 2015-01-23 19:35:12 +0000 | [diff] [blame] | 71 | .. versionchanged:: 3.4 |
| 72 | In earlier versions, if the target directory already existed, an error was |
TROUVERIE Joachim | e8eb972 | 2018-02-18 17:52:36 +0100 | [diff] [blame] | 73 | raised, unless the ``--clear`` or ``--upgrade`` option was provided. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 74 | |
Steve Dower | a1f9a33 | 2019-01-30 13:49:14 -0800 | [diff] [blame] | 75 | .. 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 Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 80 | The created ``pyvenv.cfg`` file also includes the |
| 81 | ``include-system-site-packages`` key, set to ``true`` if ``venv`` is |
| 82 | run with the ``--system-site-packages`` option, ``false`` otherwise. |
| 83 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 84 | Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be |
| 85 | invoked to bootstrap ``pip`` into the virtual environment. |
| 86 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 87 | Multiple paths can be given to ``venv``, in which case an identical virtual |
| 88 | environment will be created, according to the given options, at each provided |
| 89 | path. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 90 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 91 | Once a virtual environment has been created, it can be "activated" using a |
| 92 | script in the virtual environment's binary directory. The invocation of the |
Julien Palard | d936a8f | 2018-11-21 09:40:05 +0100 | [diff] [blame] | 93 | script is platform-specific (`<venv>` must be replaced by the path of the |
| 94 | directory containing the virtual environment): |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 95 | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 96 | +-------------+-----------------+-----------------------------------------+ |
| 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 Peksag | 6803f35 | 2016-06-27 13:10:47 +0300 | [diff] [blame] | 105 | | Windows | cmd.exe | C:\\> <venv>\\Scripts\\activate.bat | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 106 | +-------------+-----------------+-----------------------------------------+ |
Berker Peksag | 6803f35 | 2016-06-27 13:10:47 +0300 | [diff] [blame] | 107 | | | PowerShell | PS C:\\> <venv>\\Scripts\\Activate.ps1 | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 108 | +-------------+-----------------+-----------------------------------------+ |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 109 | |
| 110 | You don't specifically *need* to activate an environment; activation just |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 111 | prepends the virtual environment's binary directory to your path, so that |
| 112 | "python" invokes the virtual environment's Python interpreter and you can run |
| 113 | installed scripts without having to use their full path. However, all scripts |
| 114 | installed in a virtual environment should be runnable without activating it, |
| 115 | and run with the virtual environment's Python automatically. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 116 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 117 | You can deactivate a virtual environment by typing "deactivate" in your shell. |
| 118 | The exact mechanism is platform-specific: for example, the Bash activation |
| 119 | script defines a "deactivate" function, whereas on Windows there are separate |
| 120 | scripts called ``deactivate.bat`` and ``Deactivate.ps1`` which are installed |
| 121 | when the virtual environment is created. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 122 | |
R David Murray | 575fb31 | 2013-12-25 23:21:03 -0500 | [diff] [blame] | 123 | .. versionadded:: 3.4 |
| 124 | ``fish`` and ``csh`` activation scripts. |