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 | |
| 53 | usage: pyvenv [-h] [--system-site-packages] [--symlink] [--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. |
| 65 | --symlink Attempt to symlink rather than copy. |
| 66 | --clear Delete the environment directory if it already exists. |
| 67 | If not specified and the directory exists, an error is |
| 68 | raised. |
Vinay Sajip | 4126a7d | 2012-05-29 12:52:14 +0100 | [diff] [blame] | 69 | --upgrade Upgrade the environment directory to use this version |
| 70 | of Python, assuming Python has been upgraded in-place. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 71 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 72 | If the target directory already exists an error will be raised, unless the |
| 73 | ``--clear`` or ``--upgrade`` option was provided. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 74 | |
| 75 | The created ``pyvenv.cfg`` file also includes the |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 76 | ``include-system-site-packages`` key, set to ``true`` if ``venv`` is run with |
| 77 | the ``--system-site-packages`` option, ``false`` otherwise. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 78 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 79 | Multiple paths can be given to ``pyvenv``, in which case an identical virtualenv |
| 80 | will be created, according to the given options, at each provided path. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 81 | |
| 82 | |
| 83 | API |
| 84 | --- |
| 85 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 86 | .. highlight:: python |
| 87 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 88 | The high-level method described above makes use of a simple API which provides |
| 89 | mechanisms for third-party virtual environment creators to customize environment |
| 90 | creation according to their needs, the :class:`EnvBuilder` class. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 91 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 92 | .. class:: EnvBuilder(system_site_packages=False, clear=False, symlinks=False, upgrade=False) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 93 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 94 | The :class:`EnvBuilder` class accepts the following keyword arguments on |
| 95 | instantiation: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 96 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 97 | * ``system_site_packages`` -- a Boolean value indicating that the system Python |
| 98 | site-packages should be available to the environment (defaults to ``False``). |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 99 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 100 | * ``clear`` -- a Boolean value which, if True, will delete any existing target |
| 101 | directory instead of raising an exception (defaults to ``False``). |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 102 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 103 | * ``symlinks`` -- a Boolean value indicating whether to attempt to symlink the |
| 104 | Python binary (and any necessary DLLs or other binaries, |
| 105 | e.g. ``pythonw.exe``), rather than copying. Defaults to ``True`` on Linux and |
| 106 | Unix systems, but ``False`` on Windows and Mac OS X. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 107 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 108 | .. XXX it also takes "upgrade"! |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 109 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 110 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 111 | Creators of third-party virtual environment tools will be free to use the |
| 112 | provided ``EnvBuilder`` class as a base class. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 113 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 114 | The returned env-builder is an object which has a method, ``create``: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 115 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 116 | .. method:: create(env_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 117 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 118 | This method takes as required argument the path (absolute or relative to |
| 119 | the current directory) of the target directory which is to contain the |
| 120 | virtual environment. The ``create`` method will either create the |
| 121 | environment in the specified directory, or raise an appropriate |
| 122 | exception. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 123 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 124 | The ``create`` method of the ``EnvBuilder`` class illustrates the hooks |
| 125 | available for subclass customization:: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 126 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 127 | def create(self, env_dir): |
| 128 | """ |
| 129 | Create a virtualized Python environment in a directory. |
| 130 | env_dir is the target directory to create an environment in. |
| 131 | """ |
| 132 | env_dir = os.path.abspath(env_dir) |
| 133 | context = self.create_directories(env_dir) |
| 134 | self.create_configuration(context) |
| 135 | self.setup_python(context) |
| 136 | self.setup_scripts(context) |
| 137 | self.post_setup(context) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 138 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 139 | Each of the methods :meth:`create_directories`, |
| 140 | :meth:`create_configuration`, :meth:`setup_python`, |
| 141 | :meth:`setup_scripts` and :meth:`post_setup` can be overridden. |
| 142 | |
| 143 | .. method:: create_directories(env_dir) |
| 144 | |
| 145 | Creates the environment directory and all necessary directories, and |
| 146 | returns a context object. This is just a holder for attributes (such as |
| 147 | paths), for use by the other methods. |
| 148 | |
| 149 | .. method:: create_configuration(context) |
| 150 | |
| 151 | Creates the ``pyvenv.cfg`` configuration file in the environment. |
| 152 | |
| 153 | .. method:: setup_python(context) |
| 154 | |
| 155 | Creates a copy of the Python executable (and, under Windows, DLLs) in |
| 156 | the environment. |
| 157 | |
| 158 | .. method:: setup_scripts(context) |
| 159 | |
| 160 | Installs activation scripts appropriate to the platform into the virtual |
| 161 | environment. |
| 162 | |
| 163 | .. method:: post_setup(context) |
| 164 | |
| 165 | A placeholder method which can be overridden in third party |
| 166 | implementations to pre-install packages in the virtual environment or |
| 167 | perform other post-creation steps. |
| 168 | |
| 169 | In addition, :class:`EnvBuilder` provides this utility method that can be |
| 170 | called from :meth:`setup_scripts` or :meth:`post_setup` in subclasses to |
| 171 | assist in installing custom scripts into the virtual environment. |
| 172 | |
| 173 | .. method:: install_scripts(context, path) |
| 174 | |
| 175 | *path* is the path to a directory that should contain subdirectories |
| 176 | "common", "posix", "nt", each containing scripts destined for the bin |
| 177 | directory in the environment. The contents of "common" and the |
| 178 | directory corresponding to :data:`os.name` are copied after some text |
| 179 | replacement of placeholders: |
| 180 | |
| 181 | * ``__VENV_DIR__`` is replaced with the absolute path of the environment |
| 182 | directory. |
| 183 | |
| 184 | * ``__VENV_NAME__`` is replaced with the environment name (final path |
| 185 | segment of environment directory). |
| 186 | |
| 187 | * ``__VENV_BIN_NAME__`` is replaced with the name of the bin directory |
| 188 | (either ``bin`` or ``Scripts``). |
| 189 | |
| 190 | * ``__VENV_PYTHON__`` is replaced with the absolute path of the |
| 191 | environment's executable. |
| 192 | |
| 193 | |
| 194 | There is also a module-level convenience function: |
| 195 | |
| 196 | .. function:: create(env_dir, system_site_packages=False, clear=False, symlinks=False) |
| 197 | |
| 198 | Create an :class:`EnvBuilder` with the given keyword arguments, and call its |
| 199 | :meth:`~EnvBuilder.create` method with the *env_dir* argument. |