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 | |
| 9 | Creation of virtual environments is done by executing the ``pyvenv`` |
| 10 | script:: |
| 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 | |
| 75 | c:\> <venv>/Scripts/activate |
| 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 | |
Vinay Sajip | 3575f91 | 2012-07-03 17:26:55 +0100 | [diff] [blame] | 95 | .. note:: A virtual environment (also called a ``venv``) is a Python |
| 96 | environment such that the Python interpreter, libraries and scripts |
| 97 | installed into it are isolated from those installed in other virtual |
| 98 | environments, and (by default) any libraries installed in a "system" Python, |
| 99 | i.e. one which is installed as part of your operating system. |
| 100 | |
| 101 | A venv is a directory tree which contains Python executable files and |
| 102 | other files which indicate that it is a venv. |
| 103 | |
| 104 | Common installation tools such as ``distribute`` and ``pip`` work as |
| 105 | expected with venvs - i.e. when a venv is active, they install Python |
| 106 | packages into the venv without needing to be told to do so explicitly. |
| 107 | |
| 108 | When a venv is active (i.e. the venv's Python interpreter is running), the |
| 109 | attributes :attr:`sys.prefix` and :attr:`sys.exec_prefix` point to the base |
| 110 | directory of the venv, whereas :attr:`sys.base_prefix` and |
| 111 | :attr:`sys.base_exec_prefix` point to the non-venv Python installation |
| 112 | which was used to create the venv. If a venv is not active, then |
| 113 | :attr:`sys.prefix` is the same as :attr:`sys.base_prefix` and |
| 114 | :attr:`sys.exec_prefix` is the same as :attr:`sys.base_exec_prefix` (they |
| 115 | all point to a non-venv Python installation). |
| 116 | |