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