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