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