Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 1 | :mod:`venv` --- Creation of virtual environments |
| 2 | ================================================ |
| 3 | |
| 4 | .. module:: venv |
| 5 | :synopsis: Creation of virtual environments. |
| 6 | .. moduleauthor:: Vinay Sajip <vinay_sajip@yahoo.co.uk> |
| 7 | .. sectionauthor:: Vinay Sajip <vinay_sajip@yahoo.co.uk> |
| 8 | |
| 9 | |
| 10 | .. index:: pair: Environments; virtual |
| 11 | |
| 12 | .. versionadded:: 3.3 |
| 13 | |
| 14 | **Source code:** :source:`Lib/venv.py` |
| 15 | |
| 16 | -------------- |
| 17 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 18 | The :mod:`venv` module provides support for creating lightweight "virtual |
| 19 | environments" with their own site directories, optionally isolated from system |
| 20 | site directories. Each virtual environment has its own Python binary (allowing |
| 21 | creation of environments with various Python versions) and can have its own |
| 22 | independent set of installed Python packages in its site directories. |
| 23 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 24 | |
| 25 | Creating virtual environments |
| 26 | ----------------------------- |
| 27 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 28 | Creation of virtual environments is simplest executing the ``pyvenv`` script:: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 29 | |
| 30 | pyvenv /path/to/new/virtual/environment |
| 31 | |
| 32 | Running this command creates the target directory (creating any parent |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 33 | directories that don't exist already) and places a ``pyvenv.cfg`` file in it |
| 34 | with a ``home`` key pointing to the Python installation the command was run |
| 35 | from. It also creates a ``bin`` (or ``Scripts`` on Windows) subdirectory |
| 36 | containing a copy of the ``python`` binary (or binaries, in the case of |
| 37 | Windows). It also creates an (initially empty) ``lib/pythonX.Y/site-packages`` |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 38 | subdirectory (on Windows, this is ``Lib\site-packages``). |
| 39 | |
| 40 | .. highlight:: none |
| 41 | |
| 42 | On Windows, you may have to invoke the ``pyvenv`` script as follows, if you |
| 43 | don't have the relevant PATH and PATHEXT settings:: |
| 44 | |
| 45 | c:\Temp>c:\Python33\python c:\Python33\Tools\Scripts\pyvenv.py myenv |
| 46 | |
| 47 | or equivalently:: |
| 48 | |
| 49 | c:\Temp>c:\Python33\python -m venv myenv |
| 50 | |
| 51 | The command, if run with ``-h``, will show the available options:: |
| 52 | |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 53 | usage: pyvenv [-h] [--system-site-packages] [--symlinks] [--clear] |
Vinay Sajip | 4126a7d | 2012-05-29 12:52:14 +0100 | [diff] [blame] | 54 | [--upgrade] ENV_DIR [ENV_DIR ...] |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 55 | |
| 56 | Creates virtual Python environments in one or more target directories. |
| 57 | |
| 58 | positional arguments: |
| 59 | ENV_DIR A directory to create the environment in. |
| 60 | |
| 61 | optional arguments: |
| 62 | -h, --help show this help message and exit |
| 63 | --system-site-packages Give access to the global site-packages dir to the |
| 64 | virtual environment. |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 65 | --symlinks Try to use symlinks rather than copies, when symlinks |
| 66 | are not the default for the platform. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 67 | --clear Delete the environment directory if it already exists. |
| 68 | If not specified and the directory exists, an error is |
| 69 | raised. |
Vinay Sajip | 4126a7d | 2012-05-29 12:52:14 +0100 | [diff] [blame] | 70 | --upgrade Upgrade the environment directory to use this version |
| 71 | of Python, assuming Python has been upgraded in-place. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 72 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 73 | If the target directory already exists an error will be raised, unless the |
| 74 | ``--clear`` or ``--upgrade`` option was provided. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 75 | |
| 76 | The created ``pyvenv.cfg`` file also includes the |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 77 | ``include-system-site-packages`` key, set to ``true`` if ``venv`` is run with |
| 78 | the ``--system-site-packages`` option, ``false`` otherwise. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 79 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 80 | Multiple paths can be given to ``pyvenv``, in which case an identical virtualenv |
| 81 | will be created, according to the given options, at each provided path. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 82 | |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 83 | Once a venv has been created, it can be "activated" using a script in the |
| 84 | venv's binary directory. The invocation of the script is platform-specific: on |
| 85 | a Posix platform, you would typically do:: |
| 86 | |
| 87 | $ source <venv>/bin/activate |
| 88 | |
| 89 | whereas on Windows, you might do:: |
| 90 | |
Vinay Sajip | cd9b746 | 2012-07-09 10:37:01 +0100 | [diff] [blame] | 91 | C:\> <venv>/Scripts/activate |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 92 | |
| 93 | if you are using the ``cmd.exe`` shell, or perhaps:: |
| 94 | |
| 95 | PS C:\> <venv>/Scripts/Activate.ps1 |
| 96 | |
| 97 | if you use PowerShell. |
| 98 | |
| 99 | You don't specifically *need* to activate an environment; activation just |
| 100 | prepends the venv's binary directory to your path, so that "python" invokes the |
| 101 | venv's Python interpreter and you can run installed scripts without having to |
| 102 | use their full path. However, all scripts installed in a venv should be |
| 103 | runnable without activating it, and run with the venv's Python automatically. |
| 104 | |
| 105 | You can deactivate a venv by typing "deactivate" in your shell. The exact |
| 106 | mechanism is platform-specific: for example, the Bash activation script defines |
| 107 | a "deactivate" function, whereas on Windows there are separate scripts called |
| 108 | ``deactivate.bat`` and ``Deactivate.ps1`` which are installed when the venv is |
| 109 | created. |
| 110 | |
Vinay Sajip | cd9b746 | 2012-07-09 10:37:01 +0100 | [diff] [blame] | 111 | .. _venv-def: |
| 112 | |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 113 | .. note:: A virtual environment (also called a ``venv``) is a Python |
| 114 | environment such that the Python interpreter, libraries and scripts |
| 115 | installed into it are isolated from those installed in other virtual |
| 116 | environments, and (by default) any libraries installed in a "system" Python, |
| 117 | i.e. one which is installed as part of your operating system. |
| 118 | |
| 119 | A venv is a directory tree which contains Python executable files and |
| 120 | other files which indicate that it is a venv. |
| 121 | |
| 122 | Common installation tools such as ``distribute`` and ``pip`` work as |
| 123 | expected with venvs - i.e. when a venv is active, they install Python |
| 124 | packages into the venv without needing to be told to do so explicitly. |
| 125 | |
| 126 | When a venv is active (i.e. the venv's Python interpreter is running), the |
| 127 | attributes :attr:`sys.prefix` and :attr:`sys.exec_prefix` point to the base |
| 128 | directory of the venv, whereas :attr:`sys.base_prefix` and |
| 129 | :attr:`sys.base_exec_prefix` point to the non-venv Python installation |
| 130 | which was used to create the venv. If a venv is not active, then |
| 131 | :attr:`sys.prefix` is the same as :attr:`sys.base_prefix` and |
| 132 | :attr:`sys.exec_prefix` is the same as :attr:`sys.base_exec_prefix` (they |
| 133 | all point to a non-venv Python installation). |
| 134 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 135 | |
| 136 | API |
| 137 | --- |
| 138 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 139 | .. highlight:: python |
| 140 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 141 | The high-level method described above makes use of a simple API which provides |
| 142 | mechanisms for third-party virtual environment creators to customize environment |
| 143 | creation according to their needs, the :class:`EnvBuilder` class. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 144 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 145 | .. class:: EnvBuilder(system_site_packages=False, clear=False, symlinks=False, upgrade=False) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 146 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 147 | The :class:`EnvBuilder` class accepts the following keyword arguments on |
| 148 | instantiation: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 149 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 150 | * ``system_site_packages`` -- a Boolean value indicating that the system Python |
| 151 | site-packages should be available to the environment (defaults to ``False``). |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 152 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 153 | * ``clear`` -- a Boolean value which, if True, will delete any existing target |
| 154 | directory instead of raising an exception (defaults to ``False``). |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 155 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 156 | * ``symlinks`` -- a Boolean value indicating whether to attempt to symlink the |
| 157 | Python binary (and any necessary DLLs or other binaries, |
| 158 | e.g. ``pythonw.exe``), rather than copying. Defaults to ``True`` on Linux and |
| 159 | Unix systems, but ``False`` on Windows and Mac OS X. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 160 | |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 161 | * ``upgrade`` -- a Boolean value which, if True, will upgrade an existing |
| 162 | environment with the running Python - for use when that Python has been |
| 163 | upgraded in-place (defaults to ``False``). |
| 164 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 165 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 166 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 167 | Creators of third-party virtual environment tools will be free to use the |
| 168 | provided ``EnvBuilder`` class as a base class. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 169 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 170 | The returned env-builder is an object which has a method, ``create``: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 171 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 172 | .. method:: create(env_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 173 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 174 | This method takes as required argument the path (absolute or relative to |
| 175 | the current directory) of the target directory which is to contain the |
| 176 | virtual environment. The ``create`` method will either create the |
| 177 | environment in the specified directory, or raise an appropriate |
| 178 | exception. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 179 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 180 | The ``create`` method of the ``EnvBuilder`` class illustrates the hooks |
| 181 | available for subclass customization:: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 182 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 183 | def create(self, env_dir): |
| 184 | """ |
| 185 | Create a virtualized Python environment in a directory. |
| 186 | env_dir is the target directory to create an environment in. |
| 187 | """ |
| 188 | env_dir = os.path.abspath(env_dir) |
| 189 | context = self.create_directories(env_dir) |
| 190 | self.create_configuration(context) |
| 191 | self.setup_python(context) |
| 192 | self.setup_scripts(context) |
| 193 | self.post_setup(context) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 194 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 195 | Each of the methods :meth:`create_directories`, |
| 196 | :meth:`create_configuration`, :meth:`setup_python`, |
| 197 | :meth:`setup_scripts` and :meth:`post_setup` can be overridden. |
| 198 | |
| 199 | .. method:: create_directories(env_dir) |
| 200 | |
| 201 | Creates the environment directory and all necessary directories, and |
| 202 | returns a context object. This is just a holder for attributes (such as |
| 203 | paths), for use by the other methods. |
| 204 | |
| 205 | .. method:: create_configuration(context) |
| 206 | |
| 207 | Creates the ``pyvenv.cfg`` configuration file in the environment. |
| 208 | |
| 209 | .. method:: setup_python(context) |
| 210 | |
| 211 | Creates a copy of the Python executable (and, under Windows, DLLs) in |
| 212 | the environment. |
| 213 | |
| 214 | .. method:: setup_scripts(context) |
| 215 | |
| 216 | Installs activation scripts appropriate to the platform into the virtual |
| 217 | environment. |
| 218 | |
| 219 | .. method:: post_setup(context) |
| 220 | |
| 221 | A placeholder method which can be overridden in third party |
| 222 | implementations to pre-install packages in the virtual environment or |
| 223 | perform other post-creation steps. |
| 224 | |
| 225 | In addition, :class:`EnvBuilder` provides this utility method that can be |
| 226 | called from :meth:`setup_scripts` or :meth:`post_setup` in subclasses to |
| 227 | assist in installing custom scripts into the virtual environment. |
| 228 | |
| 229 | .. method:: install_scripts(context, path) |
| 230 | |
| 231 | *path* is the path to a directory that should contain subdirectories |
| 232 | "common", "posix", "nt", each containing scripts destined for the bin |
| 233 | directory in the environment. The contents of "common" and the |
| 234 | directory corresponding to :data:`os.name` are copied after some text |
| 235 | replacement of placeholders: |
| 236 | |
| 237 | * ``__VENV_DIR__`` is replaced with the absolute path of the environment |
| 238 | directory. |
| 239 | |
| 240 | * ``__VENV_NAME__`` is replaced with the environment name (final path |
| 241 | segment of environment directory). |
| 242 | |
| 243 | * ``__VENV_BIN_NAME__`` is replaced with the name of the bin directory |
| 244 | (either ``bin`` or ``Scripts``). |
| 245 | |
| 246 | * ``__VENV_PYTHON__`` is replaced with the absolute path of the |
| 247 | environment's executable. |
| 248 | |
| 249 | |
| 250 | There is also a module-level convenience function: |
| 251 | |
| 252 | .. function:: create(env_dir, system_site_packages=False, clear=False, symlinks=False) |
| 253 | |
| 254 | Create an :class:`EnvBuilder` with the given keyword arguments, and call its |
| 255 | :meth:`~EnvBuilder.create` method with the *env_dir* argument. |