Vinay Sajip | 4126a7d | 2012-05-29 12:52:14 +0100 | [diff] [blame] | 1 | .. _tools-and-scripts: |
| 2 | |
| 3 | Additional Tools and Scripts |
| 4 | ============================ |
| 5 | |
| 6 | pyvenv - Creating virtual environments |
| 7 | -------------------------------------- |
| 8 | |
Vinay Sajip | cd9b746 | 2012-07-09 10:37:01 +0100 | [diff] [blame] | 9 | Creation of :ref:`virtual environments <venv-def>` is done by executing the |
| 10 | ``pyvenv`` script:: |
Vinay Sajip | 4126a7d | 2012-05-29 12:52:14 +0100 | [diff] [blame] | 11 | |
| 12 | pyvenv /path/to/new/virtual/environment |
| 13 | |
| 14 | Running this command creates the target directory (creating any parent |
| 15 | directories that don't exist already) and places a ``pyvenv.cfg`` file |
| 16 | in it with a ``home`` key pointing to the Python installation the |
| 17 | command was run from. It also creates a ``bin`` (or ``Scripts`` on |
| 18 | Windows) subdirectory containing a copy of the ``python`` binary (or |
Éric Araujo | 859aad6 | 2012-06-24 00:07:41 -0400 | [diff] [blame] | 19 | binaries, in the case of Windows). |
Vinay Sajip | 4126a7d | 2012-05-29 12:52:14 +0100 | [diff] [blame] | 20 | It also creates an (initially empty) ``lib/pythonX.Y/site-packages`` |
| 21 | subdirectory (on Windows, this is ``Lib\site-packages``). |
| 22 | |
| 23 | .. highlight:: none |
| 24 | |
| 25 | On Windows, you may have to invoke the ``pyvenv`` script as follows, if you |
| 26 | don't have the relevant PATH and PATHEXT settings:: |
| 27 | |
| 28 | c:\Temp>c:\Python33\python c:\Python33\Tools\Scripts\pyvenv.py myenv |
| 29 | |
| 30 | or equivalently:: |
| 31 | |
| 32 | c:\Temp>c:\Python33\python -m venv myenv |
| 33 | |
| 34 | The command, if run with ``-h``, will show the available options:: |
| 35 | |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 36 | usage: pyvenv [-h] [--system-site-packages] [--symlinks] [--clear] |
Vinay Sajip | 4126a7d | 2012-05-29 12:52:14 +0100 | [diff] [blame] | 37 | [--upgrade] ENV_DIR [ENV_DIR ...] |
| 38 | |
| 39 | Creates virtual Python environments in one or more target directories. |
| 40 | |
| 41 | positional arguments: |
| 42 | ENV_DIR A directory to create the environment in. |
| 43 | |
| 44 | optional arguments: |
| 45 | -h, --help show this help message and exit |
| 46 | --system-site-packages Give access to the global site-packages dir to the |
| 47 | virtual environment. |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 48 | --symlinks Try to use symlinks rather than copies, when symlinks |
| 49 | are not the default for the platform. |
Vinay Sajip | 4126a7d | 2012-05-29 12:52:14 +0100 | [diff] [blame] | 50 | --clear Delete the environment directory if it already exists. |
| 51 | If not specified and the directory exists, an error is |
| 52 | raised. |
| 53 | --upgrade Upgrade the environment directory to use this version |
| 54 | of Python, assuming Python has been upgraded in-place. |
| 55 | |
| 56 | If the target directory already exists an error will be raised, unless |
| 57 | the ``--clear`` or ``--upgrade`` option was provided. |
| 58 | |
| 59 | The created ``pyvenv.cfg`` file also includes the |
| 60 | ``include-system-site-packages`` key, set to ``true`` if ``venv`` is |
| 61 | run with the ``--system-site-packages`` option, ``false`` otherwise. |
| 62 | |
| 63 | Multiple paths can be given to ``pyvenv``, in which case an identical |
| 64 | virtualenv will be created, according to the given options, at each |
| 65 | provided path. |
| 66 | |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 67 | Once a venv has been created, it can be "activated" using a script in the |
| 68 | venv's binary directory. The invocation of the script is platform-specific: on |
| 69 | a Posix platform, you would typically do:: |
| 70 | |
| 71 | $ source <venv>/bin/activate |
| 72 | |
| 73 | whereas on Windows, you might do:: |
| 74 | |
Vinay Sajip | cd9b746 | 2012-07-09 10:37:01 +0100 | [diff] [blame] | 75 | C:\> <venv>/Scripts/activate |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 76 | |
| 77 | if you are using the ``cmd.exe`` shell, or perhaps:: |
| 78 | |
| 79 | PS C:\> <venv>/Scripts/Activate.ps1 |
| 80 | |
| 81 | if you use PowerShell. |
| 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 | |