|
| 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 | + |
| 18 | +The :mod:`venv` module provides support for creating lightweight |
| 19 | +"virtual environments" with their own site directories, optionally |
| 20 | +isolated from system site directories. Each virtual environment has |
| 21 | +its own Python binary (allowing creation of environments with various |
| 22 | +Python versions) and can have its own independent set of installed |
| 23 | +Python packages in its site directories. |
| 24 | + |
| 25 | +Creating virtual environments |
| 26 | +----------------------------- |
| 27 | + |
| 28 | +Creation of virtual environments is simplest executing the ``pyvenv`` |
| 29 | +script:: |
| 30 | + |
| 31 | + pyvenv /path/to/new/virtual/environment |
| 32 | + |
| 33 | +Running this command creates the target directory (creating any parent |
| 34 | +directories that don't exist already) and places a ``pyvenv.cfg`` file |
| 35 | +in it with a ``home`` key pointing to the Python installation the |
| 36 | +command was run from. It also creates a ``bin`` (or ``Scripts`` on |
| 37 | +Windows) subdirectory containing a copy of the ``python`` binary (or |
| 38 | +binaries, in the case of Windows) and the ``pysetup3`` script (to |
| 39 | +facilitate easy installation of packages from PyPI into the new virtualenv). |
| 40 | +It also creates an (initially empty) ``lib/pythonX.Y/site-packages`` |
| 41 | +subdirectory (on Windows, this is ``Lib\site-packages``). |
| 42 | + |
| 43 | +.. highlight:: none |
| 44 | + |
| 45 | +On Windows, you may have to invoke the ``pyvenv`` script as follows, if you |
| 46 | +don't have the relevant PATH and PATHEXT settings:: |
| 47 | + |
| 48 | + c:\Temp>c:\Python33\python c:\Python33\Tools\Scripts\pyvenv.py myenv |
| 49 | + |
| 50 | +or equivalently:: |
| 51 | + |
| 52 | + c:\Temp>c:\Python33\python -m venv myenv |
| 53 | + |
| 54 | +The command, if run with ``-h``, will show the available options:: |
| 55 | + |
| 56 | + usage: pyvenv [-h] [--system-site-packages] [--symlink] [--clear] |
| 57 | + ENV_DIR [ENV_DIR ...] |
| 58 | + |
| 59 | + Creates virtual Python environments in one or more target directories. |
| 60 | + |
| 61 | + positional arguments: |
| 62 | + ENV_DIR A directory to create the environment in. |
| 63 | + |
| 64 | + optional arguments: |
| 65 | + -h, --help show this help message and exit |
| 66 | + --system-site-packages Give access to the global site-packages dir to the |
| 67 | + virtual environment. |
| 68 | + --symlink Attempt to symlink rather than copy. |
| 69 | + --clear Delete the environment directory if it already exists. |
| 70 | + If not specified and the directory exists, an error is |
| 71 | + raised. |
| 72 | + |
| 73 | + |
| 74 | +If the target directory already exists an error will be raised, unless |
| 75 | +the ``--clear`` option was provided, in which case the target |
| 76 | +directory will be deleted and virtual environment creation will |
| 77 | +proceed as usual. |
| 78 | + |
| 79 | +The created ``pyvenv.cfg`` file also includes the |
| 80 | +``include-system-site-packages`` key, set to ``true`` if ``venv`` is |
| 81 | +run with the ``--system-site-packages`` option, ``false`` otherwise. |
| 82 | + |
| 83 | +Multiple paths can be given to ``pyvenv``, in which case an identical |
| 84 | +virtualenv will be created, according to the given options, at each |
| 85 | +provided path. |
| 86 | + |
| 87 | + |
| 88 | +API |
| 89 | +--- |
| 90 | + |
| 91 | +The high-level method described above makes use of a simple API which provides |
| 92 | +mechanisms for third-party virtual environment creators to customize |
| 93 | +environment creation according to their needs. |
| 94 | + |
| 95 | +The :class:`EnvBuilder` class accepts the following keyword arguments on |
| 96 | +instantiation: |
| 97 | + |
| 98 | + * ``system_site_packages`` - A Boolean value indicating that the |
| 99 | + system Python site-packages should be available to the |
| 100 | + environment (defaults to ``False``). |
| 101 | + |
| 102 | + * ``clear`` - A Boolean value which, if True, will delete any |
| 103 | + existing target directory instead of raising an exception |
| 104 | + (defaults to ``False``). |
| 105 | + |
| 106 | + * ``symlinks`` - A Boolean value indicating whether to attempt |
| 107 | + to symlink the Python binary (and any necessary DLLs or other |
| 108 | + binaries, e.g. ``pythonw.exe``), rather than copying. Defaults to |
| 109 | + ``True`` on Linux and Unix systems, but ``False`` on Windows and |
| 110 | + Mac OS X. |
| 111 | + |
| 112 | +The returned env-builder is an object which has a method, ``create``, |
| 113 | +which takes as required argument the path (absolute or relative to the current |
| 114 | +directory) of the target directory which is to contain the virtual environment. |
| 115 | +The ``create`` method will either create the environment in the specified |
| 116 | +directory, or raise an appropriate exception. |
| 117 | + |
| 118 | +Creators of third-party virtual environment tools will be free to use |
| 119 | +the provided ``EnvBuilder`` class as a base class. |
| 120 | + |
| 121 | +.. highlight:: python |
| 122 | + |
| 123 | +The ``venv`` module will also provide a module-level function as a |
| 124 | +convenience:: |
| 125 | + |
| 126 | + def create(env_dir, |
| 127 | + system_site_packages=False, clear=False, symlinks=False): |
| 128 | + builder = EnvBuilder( |
| 129 | + system_site_packages=system_site_packages, |
| 130 | + clear=clear, |
| 131 | + symlinks=symlinks) |
| 132 | + builder.create(env_dir) |
| 133 | + |
| 134 | +The ``create`` method of the ``EnvBuilder`` class illustrates the |
| 135 | +hooks available for subclass customization:: |
| 136 | + |
| 137 | + def create(self, env_dir): |
| 138 | + """ |
| 139 | + Create a virtualized Python environment in a directory. |
| 140 | + |
| 141 | + :param env_dir: The target directory to create an environment in. |
| 142 | + |
| 143 | + """ |
| 144 | + env_dir = os.path.abspath(env_dir) |
| 145 | + context = self.create_directories(env_dir) |
| 146 | + self.create_configuration(context) |
| 147 | + self.setup_python(context) |
| 148 | + self.setup_scripts(context) |
| 149 | + self.post_setup(context) |
| 150 | + |
| 151 | +Each of the methods ``create_directories``, ``create_configuration``, |
| 152 | +``setup_python``, ``setup_scripts`` and ``post_setup`` can be |
| 153 | +overridden. The functions of these methods are: |
| 154 | + |
| 155 | + * ``create_directories`` - creates the environment directory and |
| 156 | + all necessary directories, and returns a context object. This is |
| 157 | + just a holder for attributes (such as paths), for use by the |
| 158 | + other methods. |
| 159 | + |
| 160 | + * ``create_configuration`` - creates the ``pyvenv.cfg`` |
| 161 | + configuration file in the environment. |
| 162 | + |
| 163 | + * ``setup_python`` - creates a copy of the Python executable (and, |
| 164 | + under Windows, DLLs) in the environment. |
| 165 | + |
| 166 | + * ``setup_scripts`` - Installs activation scripts appropriate to the |
| 167 | + platform into the virtual environment. |
| 168 | + |
| 169 | + * ``post_setup`` - A placeholder method which can be overridden |
| 170 | + in third party implementations to pre-install packages in the |
| 171 | + virtual environment or perform other post-creation steps. |
| 172 | + |
| 173 | +In addition, ``EnvBuilder`` provides an ``install_scripts`` utility |
| 174 | +method that can be called from ``setup_scripts`` or ``post_setup`` in |
| 175 | +subclasses to assist in installing custom scripts into the virtual |
| 176 | +environment. The method accepts as arguments the context object (see |
| 177 | +above) and a path to a directory. The directory should contain |
| 178 | +subdirectories "common", "posix", "nt", each containing scripts |
| 179 | +destined for the bin directory in the environment. The contents of |
| 180 | +"common" and the directory corresponding to ``os.name`` are copied |
| 181 | +after some text replacement of placeholders: |
| 182 | + |
| 183 | +* ``__VENV_DIR__`` is replaced with the absolute path of the |
| 184 | + environment directory. |
| 185 | + |
| 186 | +* ``__VENV_NAME__`` is replaced with the environment name (final path |
| 187 | + segment of environment directory). |
| 188 | + |
| 189 | +* ``__VENV_BIN_NAME__`` is replaced with the name of the bin directory |
| 190 | + (either ``bin`` or ``Scripts``). |
| 191 | + |
| 192 | +* ``__VENV_PYTHON__`` is replaced with the absolute path of the |
| 193 | + environment's executable. |
0 commit comments