blob: e5c91e462016aac0c62124250dc016f09a1d184c [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
Georg Brandldbab58f2012-06-24 16:37:59 +020018The :mod:`venv` module provides support for creating lightweight "virtual
19environments" with their own site directories, optionally isolated from system
20site directories. Each virtual environment has its own Python binary (allowing
21creation of environments with various Python versions) and can have its own
22independent set of installed Python packages in its site directories.
23
Vinay Sajip7ded1f02012-05-26 03:45:29 +010024
25Creating virtual environments
26-----------------------------
27
Georg Brandldbab58f2012-06-24 16:37:59 +020028Creation of virtual environments is simplest executing the ``pyvenv`` script::
Vinay Sajip7ded1f02012-05-26 03:45:29 +010029
30 pyvenv /path/to/new/virtual/environment
31
32Running this command creates the target directory (creating any parent
Georg Brandldbab58f2012-06-24 16:37:59 +020033directories that don't exist already) and places a ``pyvenv.cfg`` file in it
34with a ``home`` key pointing to the Python installation the command was run
35from. It also creates a ``bin`` (or ``Scripts`` on Windows) subdirectory
36containing a copy of the ``python`` binary (or binaries, in the case of
37Windows). It also creates an (initially empty) ``lib/pythonX.Y/site-packages``
Vinay Sajip7ded1f02012-05-26 03:45:29 +010038subdirectory (on Windows, this is ``Lib\site-packages``).
39
40.. highlight:: none
41
42On Windows, you may have to invoke the ``pyvenv`` script as follows, if you
43don't have the relevant PATH and PATHEXT settings::
44
45 c:\Temp>c:\Python33\python c:\Python33\Tools\Scripts\pyvenv.py myenv
46
47or equivalently::
48
49 c:\Temp>c:\Python33\python -m venv myenv
50
51The command, if run with ``-h``, will show the available options::
52
53 usage: pyvenv [-h] [--system-site-packages] [--symlink] [--clear]
Vinay Sajip4126a7d2012-05-29 12:52:14 +010054 [--upgrade] ENV_DIR [ENV_DIR ...]
Vinay Sajip7ded1f02012-05-26 03:45:29 +010055
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 Sajip4126a7d2012-05-29 12:52:14 +010069 --upgrade Upgrade the environment directory to use this version
70 of Python, assuming Python has been upgraded in-place.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010071
Georg Brandldbab58f2012-06-24 16:37:59 +020072If the target directory already exists an error will be raised, unless the
73``--clear`` or ``--upgrade`` option was provided.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010074
75The created ``pyvenv.cfg`` file also includes the
Georg Brandldbab58f2012-06-24 16:37:59 +020076``include-system-site-packages`` key, set to ``true`` if ``venv`` is run with
77the ``--system-site-packages`` option, ``false`` otherwise.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010078
Georg Brandldbab58f2012-06-24 16:37:59 +020079Multiple paths can be given to ``pyvenv``, in which case an identical virtualenv
80will be created, according to the given options, at each provided path.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010081
82
83API
84---
85
Vinay Sajip7ded1f02012-05-26 03:45:29 +010086.. highlight:: python
87
Georg Brandldbab58f2012-06-24 16:37:59 +020088The high-level method described above makes use of a simple API which provides
89mechanisms for third-party virtual environment creators to customize environment
90creation according to their needs, the :class:`EnvBuilder` class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010091
Georg Brandldbab58f2012-06-24 16:37:59 +020092.. class:: EnvBuilder(system_site_packages=False, clear=False, symlinks=False, upgrade=False)
Vinay Sajip7ded1f02012-05-26 03:45:29 +010093
Georg Brandldbab58f2012-06-24 16:37:59 +020094 The :class:`EnvBuilder` class accepts the following keyword arguments on
95 instantiation:
Vinay Sajip7ded1f02012-05-26 03:45:29 +010096
Georg Brandldbab58f2012-06-24 16:37:59 +020097 * ``system_site_packages`` -- a Boolean value indicating that the system Python
98 site-packages should be available to the environment (defaults to ``False``).
Vinay Sajip7ded1f02012-05-26 03:45:29 +010099
Georg Brandldbab58f2012-06-24 16:37:59 +0200100 * ``clear`` -- a Boolean value which, if True, will delete any existing target
101 directory instead of raising an exception (defaults to ``False``).
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100102
Georg Brandldbab58f2012-06-24 16:37:59 +0200103 * ``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 Sajip7ded1f02012-05-26 03:45:29 +0100107
Georg Brandldbab58f2012-06-24 16:37:59 +0200108 .. XXX it also takes "upgrade"!
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100109
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100110
Georg Brandldbab58f2012-06-24 16:37:59 +0200111 Creators of third-party virtual environment tools will be free to use the
112 provided ``EnvBuilder`` class as a base class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100113
Georg Brandldbab58f2012-06-24 16:37:59 +0200114 The returned env-builder is an object which has a method, ``create``:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100115
Georg Brandldbab58f2012-06-24 16:37:59 +0200116 .. method:: create(env_dir)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100117
Georg Brandldbab58f2012-06-24 16:37:59 +0200118 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 Sajip7ded1f02012-05-26 03:45:29 +0100123
Georg Brandldbab58f2012-06-24 16:37:59 +0200124 The ``create`` method of the ``EnvBuilder`` class illustrates the hooks
125 available for subclass customization::
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100126
Georg Brandldbab58f2012-06-24 16:37:59 +0200127 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 Sajip7ded1f02012-05-26 03:45:29 +0100138
Georg Brandldbab58f2012-06-24 16:37:59 +0200139 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
194There 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.