blob: f761a002c0c932db4d9b29c01a8dadc483097398 [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
Vinay Sajipa945ad12012-07-09 09:24:59 +010053 usage: pyvenv [-h] [--system-site-packages] [--symlinks] [--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.
Vinay Sajipa945ad12012-07-09 09:24:59 +010065 --symlinks Try to use symlinks rather than copies, when symlinks
66 are not the default for the platform.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010067 --clear Delete the environment directory if it already exists.
68 If not specified and the directory exists, an error is
69 raised.
Vinay Sajip4126a7d2012-05-29 12:52:14 +010070 --upgrade Upgrade the environment directory to use this version
71 of Python, assuming Python has been upgraded in-place.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010072
Georg Brandldbab58f2012-06-24 16:37:59 +020073If the target directory already exists an error will be raised, unless the
74``--clear`` or ``--upgrade`` option was provided.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010075
76The created ``pyvenv.cfg`` file also includes the
Georg Brandldbab58f2012-06-24 16:37:59 +020077``include-system-site-packages`` key, set to ``true`` if ``venv`` is run with
78the ``--system-site-packages`` option, ``false`` otherwise.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010079
Georg Brandldbab58f2012-06-24 16:37:59 +020080Multiple paths can be given to ``pyvenv``, in which case an identical virtualenv
81will be created, according to the given options, at each provided path.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010082
Vinay Sajipa945ad12012-07-09 09:24:59 +010083Once a venv has been created, it can be "activated" using a script in the
84venv's binary directory. The invocation of the script is platform-specific: on
85a Posix platform, you would typically do::
86
87 $ source <venv>/bin/activate
88
89whereas on Windows, you might do::
90
Vinay Sajipcd9b7462012-07-09 10:37:01 +010091 C:\> <venv>/Scripts/activate
Vinay Sajipa945ad12012-07-09 09:24:59 +010092
93if you are using the ``cmd.exe`` shell, or perhaps::
94
95 PS C:\> <venv>/Scripts/Activate.ps1
96
97if you use PowerShell.
98
99You don't specifically *need* to activate an environment; activation just
100prepends the venv's binary directory to your path, so that "python" invokes the
101venv's Python interpreter and you can run installed scripts without having to
102use their full path. However, all scripts installed in a venv should be
103runnable without activating it, and run with the venv's Python automatically.
104
105You can deactivate a venv by typing "deactivate" in your shell. The exact
106mechanism is platform-specific: for example, the Bash activation script defines
107a "deactivate" function, whereas on Windows there are separate scripts called
108``deactivate.bat`` and ``Deactivate.ps1`` which are installed when the venv is
109created.
110
Vinay Sajipcd9b7462012-07-09 10:37:01 +0100111.. _venv-def:
112
Vinay Sajipa945ad12012-07-09 09:24:59 +0100113.. note:: A virtual environment (also called a ``venv``) is a Python
114 environment such that the Python interpreter, libraries and scripts
115 installed into it are isolated from those installed in other virtual
116 environments, and (by default) any libraries installed in a "system" Python,
117 i.e. one which is installed as part of your operating system.
118
119 A venv is a directory tree which contains Python executable files and
120 other files which indicate that it is a venv.
121
122 Common installation tools such as ``distribute`` and ``pip`` work as
123 expected with venvs - i.e. when a venv is active, they install Python
124 packages into the venv without needing to be told to do so explicitly.
125
126 When a venv is active (i.e. the venv's Python interpreter is running), the
127 attributes :attr:`sys.prefix` and :attr:`sys.exec_prefix` point to the base
128 directory of the venv, whereas :attr:`sys.base_prefix` and
129 :attr:`sys.base_exec_prefix` point to the non-venv Python installation
130 which was used to create the venv. If a venv is not active, then
131 :attr:`sys.prefix` is the same as :attr:`sys.base_prefix` and
132 :attr:`sys.exec_prefix` is the same as :attr:`sys.base_exec_prefix` (they
133 all point to a non-venv Python installation).
134
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100135
136API
137---
138
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100139.. highlight:: python
140
Georg Brandldbab58f2012-06-24 16:37:59 +0200141The high-level method described above makes use of a simple API which provides
142mechanisms for third-party virtual environment creators to customize environment
143creation according to their needs, the :class:`EnvBuilder` class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100144
Georg Brandldbab58f2012-06-24 16:37:59 +0200145.. class:: EnvBuilder(system_site_packages=False, clear=False, symlinks=False, upgrade=False)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100146
Georg Brandldbab58f2012-06-24 16:37:59 +0200147 The :class:`EnvBuilder` class accepts the following keyword arguments on
148 instantiation:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100149
Georg Brandldbab58f2012-06-24 16:37:59 +0200150 * ``system_site_packages`` -- a Boolean value indicating that the system Python
151 site-packages should be available to the environment (defaults to ``False``).
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100152
Georg Brandldbab58f2012-06-24 16:37:59 +0200153 * ``clear`` -- a Boolean value which, if True, will delete any existing target
154 directory instead of raising an exception (defaults to ``False``).
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100155
Georg Brandldbab58f2012-06-24 16:37:59 +0200156 * ``symlinks`` -- a Boolean value indicating whether to attempt to symlink the
157 Python binary (and any necessary DLLs or other binaries,
158 e.g. ``pythonw.exe``), rather than copying. Defaults to ``True`` on Linux and
159 Unix systems, but ``False`` on Windows and Mac OS X.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100160
Vinay Sajipa945ad12012-07-09 09:24:59 +0100161 * ``upgrade`` -- a Boolean value which, if True, will upgrade an existing
162 environment with the running Python - for use when that Python has been
163 upgraded in-place (defaults to ``False``).
164
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100165
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100166
Georg Brandldbab58f2012-06-24 16:37:59 +0200167 Creators of third-party virtual environment tools will be free to use the
168 provided ``EnvBuilder`` class as a base class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100169
Georg Brandldbab58f2012-06-24 16:37:59 +0200170 The returned env-builder is an object which has a method, ``create``:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100171
Georg Brandldbab58f2012-06-24 16:37:59 +0200172 .. method:: create(env_dir)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100173
Georg Brandldbab58f2012-06-24 16:37:59 +0200174 This method takes as required argument the path (absolute or relative to
175 the current directory) of the target directory which is to contain the
176 virtual environment. The ``create`` method will either create the
177 environment in the specified directory, or raise an appropriate
178 exception.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100179
Georg Brandldbab58f2012-06-24 16:37:59 +0200180 The ``create`` method of the ``EnvBuilder`` class illustrates the hooks
181 available for subclass customization::
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100182
Georg Brandldbab58f2012-06-24 16:37:59 +0200183 def create(self, env_dir):
184 """
185 Create a virtualized Python environment in a directory.
186 env_dir is the target directory to create an environment in.
187 """
188 env_dir = os.path.abspath(env_dir)
189 context = self.create_directories(env_dir)
190 self.create_configuration(context)
191 self.setup_python(context)
192 self.setup_scripts(context)
193 self.post_setup(context)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100194
Georg Brandldbab58f2012-06-24 16:37:59 +0200195 Each of the methods :meth:`create_directories`,
196 :meth:`create_configuration`, :meth:`setup_python`,
197 :meth:`setup_scripts` and :meth:`post_setup` can be overridden.
198
199 .. method:: create_directories(env_dir)
200
201 Creates the environment directory and all necessary directories, and
202 returns a context object. This is just a holder for attributes (such as
203 paths), for use by the other methods.
204
205 .. method:: create_configuration(context)
206
207 Creates the ``pyvenv.cfg`` configuration file in the environment.
208
209 .. method:: setup_python(context)
210
211 Creates a copy of the Python executable (and, under Windows, DLLs) in
212 the environment.
213
214 .. method:: setup_scripts(context)
215
216 Installs activation scripts appropriate to the platform into the virtual
217 environment.
218
219 .. method:: post_setup(context)
220
221 A placeholder method which can be overridden in third party
222 implementations to pre-install packages in the virtual environment or
223 perform other post-creation steps.
224
225 In addition, :class:`EnvBuilder` provides this utility method that can be
226 called from :meth:`setup_scripts` or :meth:`post_setup` in subclasses to
227 assist in installing custom scripts into the virtual environment.
228
229 .. method:: install_scripts(context, path)
230
231 *path* is the path to a directory that should contain subdirectories
232 "common", "posix", "nt", each containing scripts destined for the bin
233 directory in the environment. The contents of "common" and the
234 directory corresponding to :data:`os.name` are copied after some text
235 replacement of placeholders:
236
237 * ``__VENV_DIR__`` is replaced with the absolute path of the environment
238 directory.
239
240 * ``__VENV_NAME__`` is replaced with the environment name (final path
241 segment of environment directory).
242
243 * ``__VENV_BIN_NAME__`` is replaced with the name of the bin directory
244 (either ``bin`` or ``Scripts``).
245
246 * ``__VENV_PYTHON__`` is replaced with the absolute path of the
247 environment's executable.
248
249
250There is also a module-level convenience function:
251
252.. function:: create(env_dir, system_site_packages=False, clear=False, symlinks=False)
253
254 Create an :class:`EnvBuilder` with the given keyword arguments, and call its
255 :meth:`~EnvBuilder.create` method with the *env_dir* argument.