blob: b86f5731e8fc4737cba6d00d72b877c91f5b9156 [file] [log] [blame]
Vinay Sajip7ded1f02012-05-26 03:45:29 +01001: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
18The :mod:`venv` module provides support for creating lightweight
19"virtual environments" with their own site directories, optionally
20isolated from system site directories. Each virtual environment has
21its own Python binary (allowing creation of environments with various
22Python versions) and can have its own independent set of installed
23Python packages in its site directories.
24
25Creating virtual environments
26-----------------------------
27
28Creation of virtual environments is simplest executing the ``pyvenv``
29script::
30
31 pyvenv /path/to/new/virtual/environment
32
33Running this command creates the target directory (creating any parent
34directories that don't exist already) and places a ``pyvenv.cfg`` file
35in it with a ``home`` key pointing to the Python installation the
36command was run from. It also creates a ``bin`` (or ``Scripts`` on
37Windows) subdirectory containing a copy of the ``python`` binary (or
38binaries, in the case of Windows) and the ``pysetup3`` script (to
39facilitate easy installation of packages from PyPI into the new virtualenv).
40It also creates an (initially empty) ``lib/pythonX.Y/site-packages``
41subdirectory (on Windows, this is ``Lib\site-packages``).
42
43.. highlight:: none
44
45On Windows, you may have to invoke the ``pyvenv`` script as follows, if you
46don't have the relevant PATH and PATHEXT settings::
47
48 c:\Temp>c:\Python33\python c:\Python33\Tools\Scripts\pyvenv.py myenv
49
50or equivalently::
51
52 c:\Temp>c:\Python33\python -m venv myenv
53
54The 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
74If the target directory already exists an error will be raised, unless
75the ``--clear`` option was provided, in which case the target
76directory will be deleted and virtual environment creation will
77proceed as usual.
78
79The created ``pyvenv.cfg`` file also includes the
80``include-system-site-packages`` key, set to ``true`` if ``venv`` is
81run with the ``--system-site-packages`` option, ``false`` otherwise.
82
83Multiple paths can be given to ``pyvenv``, in which case an identical
84virtualenv will be created, according to the given options, at each
85provided path.
86
87
88API
89---
90
91The high-level method described above makes use of a simple API which provides
92mechanisms for third-party virtual environment creators to customize
93environment creation according to their needs.
94
95The :class:`EnvBuilder` class accepts the following keyword arguments on
96instantiation:
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
112The returned env-builder is an object which has a method, ``create``,
113which takes as required argument the path (absolute or relative to the current
114directory) of the target directory which is to contain the virtual environment.
115The ``create`` method will either create the environment in the specified
116directory, or raise an appropriate exception.
117
118Creators of third-party virtual environment tools will be free to use
119the provided ``EnvBuilder`` class as a base class.
120
121.. highlight:: python
122
123The ``venv`` module will also provide a module-level function as a
124convenience::
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
134The ``create`` method of the ``EnvBuilder`` class illustrates the
135hooks 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
151Each of the methods ``create_directories``, ``create_configuration``,
152``setup_python``, ``setup_scripts`` and ``post_setup`` can be
153overridden. 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
173In addition, ``EnvBuilder`` provides an ``install_scripts`` utility
174method that can be called from ``setup_scripts`` or ``post_setup`` in
175subclasses to assist in installing custom scripts into the virtual
176environment. The method accepts as arguments the context object (see
177above) and a path to a directory. The directory should contain
178subdirectories "common", "posix", "nt", each containing scripts
179destined for the bin directory in the environment. The contents of
180"common" and the directory corresponding to ``os.name`` are copied
181after 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.