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 |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 10 | containing a copy of the ``python`` binary (or binaries, in the case of |
| 11 | Windows). It also creates an (initially empty) ``lib/pythonX.Y/site-packages`` |
| 12 | subdirectory (on Windows, this is ``Lib\site-packages``). |
| 13 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 14 | .. deprecated:: 3.6 |
| 15 | ``pyvenv`` was the recommended tool for creating virtual environments for |
| 16 | Python 3.3 and 3.4, and is `deprecated in Python 3.6 |
| 17 | <https://docs.python.org/dev/whatsnew/3.6.html#deprecated-features>`_. |
| 18 | |
| 19 | .. versionchanged:: 3.5 |
| 20 | The use of ``venv`` is now recommended for creating virtual environments. |
| 21 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 22 | .. seealso:: |
| 23 | |
| 24 | `Python Packaging User Guide: Creating and using virtual environments |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 25 | <https://packaging.python.org/installing/#creating-virtual-environments>`__ |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 26 | |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 27 | .. highlight:: none |
| 28 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 29 | On Windows, invoke the ``venv`` command as follows:: |
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 | c:\>c:\Python35\python -m venv c:\path\to\myenv |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 32 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 33 | Alternatively, if you configured the ``PATH`` and ``PATHEXT`` variables for |
| 34 | your :ref:`Python installation <using-on-windows>`:: |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 35 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 36 | c:\>python -m venv myenv c:\path\to\myenv |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 37 | |
| 38 | The command, if run with ``-h``, will show the available options:: |
| 39 | |
Berker Peksag | a9744ae | 2016-01-30 12:17:10 +0200 | [diff] [blame] | 40 | usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear] |
| 41 | [--upgrade] [--without-pip] |
| 42 | ENV_DIR [ENV_DIR ...] |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 43 | |
| 44 | Creates virtual Python environments in one or more target directories. |
| 45 | |
| 46 | positional arguments: |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 47 | ENV_DIR A directory to create the environment in. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 48 | |
| 49 | optional arguments: |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 50 | -h, --help show this help message and exit |
| 51 | --system-site-packages |
| 52 | Give the virtual environment access to the system |
| 53 | site-packages dir. |
| 54 | --symlinks Try to use symlinks rather than copies, when symlinks |
| 55 | are not the default for the platform. |
| 56 | --copies Try to use copies rather than symlinks, even when |
| 57 | symlinks are the default for the platform. |
| 58 | --clear Delete the contents of the environment directory if it |
| 59 | already exists, before environment creation. |
| 60 | --upgrade Upgrade the environment directory to use this version |
| 61 | of Python, assuming Python has been upgraded in-place. |
| 62 | --without-pip Skips installing or upgrading pip in the virtual |
| 63 | environment (pip is bootstrapped by default) |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 64 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 65 | 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 Sajip | c7e34fb | 2015-02-07 10:52:02 +0000 | [diff] [blame] | 67 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 68 | .. versionchanged:: 3.4 |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 69 | Installs pip by default, added the ``--without-pip`` and ``--copies`` |
| 70 | options |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 71 | |
Vinay Sajip | 71e7296 | 2015-01-23 19:35:12 +0000 | [diff] [blame] | 72 | .. versionchanged:: 3.4 |
| 73 | In earlier versions, if the target directory already existed, an error was |
| 74 | raised, unless the ``--clear`` or ``--upgrade`` option was provided. Now, |
| 75 | if an existing directory is specified, its contents are removed and |
| 76 | the directory is processed as if it had been newly created. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 77 | |
| 78 | The created ``pyvenv.cfg`` file also includes the |
| 79 | ``include-system-site-packages`` key, set to ``true`` if ``venv`` is |
| 80 | run with the ``--system-site-packages`` option, ``false`` otherwise. |
| 81 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 82 | Unless the ``--without-pip`` option is given, :mod:`ensurepip` will be |
| 83 | invoked to bootstrap ``pip`` into the virtual environment. |
| 84 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 85 | Multiple paths can be given to ``venv``, in which case an identical virtual |
| 86 | environment will be created, according to the given options, at each provided |
| 87 | path. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 88 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 89 | Once a virtual environment has been created, it can be "activated" using a |
| 90 | script in the virtual environment's binary directory. The invocation of the |
| 91 | script is platform-specific: |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 92 | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 93 | +-------------+-----------------+-----------------------------------------+ |
| 94 | | Platform | Shell | Command to activate virtual environment | |
| 95 | +=============+=================+=========================================+ |
| 96 | | Posix | bash/zsh | $ source <venv>/bin/activate | |
| 97 | +-------------+-----------------+-----------------------------------------+ |
| 98 | | | fish | $ . <venv>/bin/activate.fish | |
| 99 | +-------------+-----------------+-----------------------------------------+ |
| 100 | | | csh/tcsh | $ source <venv>/bin/activate.csh | |
| 101 | +-------------+-----------------+-----------------------------------------+ |
Berker Peksag | 6803f35 | 2016-06-27 13:10:47 +0300 | [diff] [blame] | 102 | | Windows | cmd.exe | C:\\> <venv>\\Scripts\\activate.bat | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 103 | +-------------+-----------------+-----------------------------------------+ |
Berker Peksag | 6803f35 | 2016-06-27 13:10:47 +0300 | [diff] [blame] | 104 | | | PowerShell | PS C:\\> <venv>\\Scripts\\Activate.ps1 | |
Andrew Svetlov | 65e9c57 | 2012-10-04 21:48:58 +0300 | [diff] [blame] | 105 | +-------------+-----------------+-----------------------------------------+ |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 106 | |
| 107 | You don't specifically *need* to activate an environment; activation just |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 108 | prepends the virtual environment's binary directory to your path, so that |
| 109 | "python" invokes the virtual environment's Python interpreter and you can run |
| 110 | installed scripts without having to use their full path. However, all scripts |
| 111 | installed in a virtual environment should be runnable without activating it, |
| 112 | and run with the virtual environment's Python automatically. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 113 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 114 | You can deactivate a virtual environment by typing "deactivate" in your shell. |
| 115 | The exact mechanism is platform-specific: for example, the Bash activation |
| 116 | script defines a "deactivate" function, whereas on Windows there are separate |
| 117 | scripts called ``deactivate.bat`` and ``Deactivate.ps1`` which are installed |
| 118 | when the virtual environment is created. |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 119 | |
R David Murray | 575fb31 | 2013-12-25 23:21:03 -0500 | [diff] [blame] | 120 | .. versionadded:: 3.4 |
| 121 | ``fish`` and ``csh`` activation scripts. |