blob: ac0115b863ec9b2ce02e25c1f888f49bf55755a2 [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
91 c:\> <venv>/Scripts/activate
92
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
111.. note:: A virtual environment (also called a ``venv``) is a Python
112 environment such that the Python interpreter, libraries and scripts
113 installed into it are isolated from those installed in other virtual
114 environments, and (by default) any libraries installed in a "system" Python,
115 i.e. one which is installed as part of your operating system.
116
117 A venv is a directory tree which contains Python executable files and
118 other files which indicate that it is a venv.
119
120 Common installation tools such as ``distribute`` and ``pip`` work as
121 expected with venvs - i.e. when a venv is active, they install Python
122 packages into the venv without needing to be told to do so explicitly.
123
124 When a venv is active (i.e. the venv's Python interpreter is running), the
125 attributes :attr:`sys.prefix` and :attr:`sys.exec_prefix` point to the base
126 directory of the venv, whereas :attr:`sys.base_prefix` and
127 :attr:`sys.base_exec_prefix` point to the non-venv Python installation
128 which was used to create the venv. If a venv is not active, then
129 :attr:`sys.prefix` is the same as :attr:`sys.base_prefix` and
130 :attr:`sys.exec_prefix` is the same as :attr:`sys.base_exec_prefix` (they
131 all point to a non-venv Python installation).
132
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100133
134API
135---
136
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100137.. highlight:: python
138
Georg Brandldbab58f2012-06-24 16:37:59 +0200139The high-level method described above makes use of a simple API which provides
140mechanisms for third-party virtual environment creators to customize environment
141creation according to their needs, the :class:`EnvBuilder` class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100142
Georg Brandldbab58f2012-06-24 16:37:59 +0200143.. class:: EnvBuilder(system_site_packages=False, clear=False, symlinks=False, upgrade=False)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100144
Georg Brandldbab58f2012-06-24 16:37:59 +0200145 The :class:`EnvBuilder` class accepts the following keyword arguments on
146 instantiation:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100147
Georg Brandldbab58f2012-06-24 16:37:59 +0200148 * ``system_site_packages`` -- a Boolean value indicating that the system Python
149 site-packages should be available to the environment (defaults to ``False``).
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100150
Georg Brandldbab58f2012-06-24 16:37:59 +0200151 * ``clear`` -- a Boolean value which, if True, will delete any existing target
152 directory instead of raising an exception (defaults to ``False``).
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100153
Georg Brandldbab58f2012-06-24 16:37:59 +0200154 * ``symlinks`` -- a Boolean value indicating whether to attempt to symlink the
155 Python binary (and any necessary DLLs or other binaries,
156 e.g. ``pythonw.exe``), rather than copying. Defaults to ``True`` on Linux and
157 Unix systems, but ``False`` on Windows and Mac OS X.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100158
Vinay Sajipa945ad12012-07-09 09:24:59 +0100159 * ``upgrade`` -- a Boolean value which, if True, will upgrade an existing
160 environment with the running Python - for use when that Python has been
161 upgraded in-place (defaults to ``False``).
162
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100163
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100164
Georg Brandldbab58f2012-06-24 16:37:59 +0200165 Creators of third-party virtual environment tools will be free to use the
166 provided ``EnvBuilder`` class as a base class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100167
Georg Brandldbab58f2012-06-24 16:37:59 +0200168 The returned env-builder is an object which has a method, ``create``:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100169
Georg Brandldbab58f2012-06-24 16:37:59 +0200170 .. method:: create(env_dir)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100171
Georg Brandldbab58f2012-06-24 16:37:59 +0200172 This method takes as required argument the path (absolute or relative to
173 the current directory) of the target directory which is to contain the
174 virtual environment. The ``create`` method will either create the
175 environment in the specified directory, or raise an appropriate
176 exception.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100177
Georg Brandldbab58f2012-06-24 16:37:59 +0200178 The ``create`` method of the ``EnvBuilder`` class illustrates the hooks
179 available for subclass customization::
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100180
Georg Brandldbab58f2012-06-24 16:37:59 +0200181 def create(self, env_dir):
182 """
183 Create a virtualized Python environment in a directory.
184 env_dir is the target directory to create an environment in.
185 """
186 env_dir = os.path.abspath(env_dir)
187 context = self.create_directories(env_dir)
188 self.create_configuration(context)
189 self.setup_python(context)
190 self.setup_scripts(context)
191 self.post_setup(context)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100192
Georg Brandldbab58f2012-06-24 16:37:59 +0200193 Each of the methods :meth:`create_directories`,
194 :meth:`create_configuration`, :meth:`setup_python`,
195 :meth:`setup_scripts` and :meth:`post_setup` can be overridden.
196
197 .. method:: create_directories(env_dir)
198
199 Creates the environment directory and all necessary directories, and
200 returns a context object. This is just a holder for attributes (such as
201 paths), for use by the other methods.
202
203 .. method:: create_configuration(context)
204
205 Creates the ``pyvenv.cfg`` configuration file in the environment.
206
207 .. method:: setup_python(context)
208
209 Creates a copy of the Python executable (and, under Windows, DLLs) in
210 the environment.
211
212 .. method:: setup_scripts(context)
213
214 Installs activation scripts appropriate to the platform into the virtual
215 environment.
216
217 .. method:: post_setup(context)
218
219 A placeholder method which can be overridden in third party
220 implementations to pre-install packages in the virtual environment or
221 perform other post-creation steps.
222
223 In addition, :class:`EnvBuilder` provides this utility method that can be
224 called from :meth:`setup_scripts` or :meth:`post_setup` in subclasses to
225 assist in installing custom scripts into the virtual environment.
226
227 .. method:: install_scripts(context, path)
228
229 *path* is the path to a directory that should contain subdirectories
230 "common", "posix", "nt", each containing scripts destined for the bin
231 directory in the environment. The contents of "common" and the
232 directory corresponding to :data:`os.name` are copied after some text
233 replacement of placeholders:
234
235 * ``__VENV_DIR__`` is replaced with the absolute path of the environment
236 directory.
237
238 * ``__VENV_NAME__`` is replaced with the environment name (final path
239 segment of environment directory).
240
241 * ``__VENV_BIN_NAME__`` is replaced with the name of the bin directory
242 (either ``bin`` or ``Scripts``).
243
244 * ``__VENV_PYTHON__`` is replaced with the absolute path of the
245 environment's executable.
246
247
248There is also a module-level convenience function:
249
250.. function:: create(env_dir, system_site_packages=False, clear=False, symlinks=False)
251
252 Create an :class:`EnvBuilder` with the given keyword arguments, and call its
253 :meth:`~EnvBuilder.create` method with the *env_dir* argument.