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