blob: def926b08526907a80659dc29c38b4039e506285 [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.
Terry Jan Reedyfa089b92016-06-11 15:02:54 -04006
Vinay Sajip7ded1f02012-05-26 03:45:29 +01007.. moduleauthor:: Vinay Sajip <vinay_sajip@yahoo.co.uk>
8.. sectionauthor:: Vinay Sajip <vinay_sajip@yahoo.co.uk>
9
Vinay Sajip7ded1f02012-05-26 03:45:29 +010010.. versionadded:: 3.3
11
Terry Jan Reedyfa089b92016-06-11 15:02:54 -040012**Source code:** :source:`Lib/venv/`
13
14.. index:: pair: Environments; virtual
Vinay Sajip7ded1f02012-05-26 03:45:29 +010015
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
Steve Dower83b449d2018-07-28 17:48:29 +010020site directories. Each virtual environment has its own Python binary (which
21matches the version of the binary that was used to create this environment) and
22can have its own independent set of installed Python packages in its site
23directories.
Georg Brandldbab58f2012-06-24 16:37:59 +020024
Vinay Sajipa7045822013-09-06 09:50:43 +010025See :pep:`405` for more information about Python virtual environments.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010026
Steve Dower83b449d2018-07-28 17:48:29 +010027.. seealso::
28
29 `Python Packaging User Guide: Creating and using virtual environments
30 <https://packaging.python.org/installing/#creating-virtual-environments>`__
31
Brett Cannonf8c15052016-10-21 12:53:40 -070032.. note::
Zachary Warebb444772016-10-25 21:35:22 -050033 The ``pyvenv`` script has been deprecated as of Python 3.6 in favor of using
Brett Cannonf8c15052016-10-21 12:53:40 -070034 ``python3 -m venv`` to help prevent any potential confusion as to which
35 Python interpreter a virtual environment will be based on.
36
37
Vinay Sajip7ded1f02012-05-26 03:45:29 +010038Creating virtual environments
39-----------------------------
40
Vinay Sajipc4618e32012-07-10 08:21:07 +010041.. include:: /using/venv-create.inc
Vinay Sajip7ded1f02012-05-26 03:45:29 +010042
Vinay Sajipa945ad12012-07-09 09:24:59 +010043
Vinay Sajipcd9b7462012-07-09 10:37:01 +010044.. _venv-def:
45
Brett Cannon15552c32016-07-08 10:46:21 -070046.. note:: A virtual environment is a Python environment such that the Python
47 interpreter, libraries and scripts installed into it are isolated from those
48 installed in other virtual environments, and (by default) any libraries
49 installed in a "system" Python, i.e., one which is installed as part of your
50 operating system.
Vinay Sajipa945ad12012-07-09 09:24:59 +010051
Brett Cannon15552c32016-07-08 10:46:21 -070052 A virtual environment is a directory tree which contains Python executable
53 files and other files which indicate that it is a virtual environment.
Vinay Sajipa945ad12012-07-09 09:24:59 +010054
Jason R. Coombs13266fb2014-05-12 22:40:49 -040055 Common installation tools such as ``Setuptools`` and ``pip`` work as
Brett Cannon15552c32016-07-08 10:46:21 -070056 expected with virtual environments. In other words, when a virtual
57 environment is active, they install Python packages into the virtual
58 environment without needing to be told to do so explicitly.
Vinay Sajipa945ad12012-07-09 09:24:59 +010059
Brett Cannon15552c32016-07-08 10:46:21 -070060 When a virtual environment is active (i.e., the virtual environment's Python
61 interpreter is running), the attributes :attr:`sys.prefix` and
62 :attr:`sys.exec_prefix` point to the base directory of the virtual
63 environment, whereas :attr:`sys.base_prefix` and
64 :attr:`sys.base_exec_prefix` point to the non-virtual environment Python
65 installation which was used to create the virtual environment. If a virtual
66 environment is not active, then :attr:`sys.prefix` is the same as
67 :attr:`sys.base_prefix` and :attr:`sys.exec_prefix` is the same as
68 :attr:`sys.base_exec_prefix` (they all point to a non-virtual environment
69 Python installation).
Vinay Sajipa945ad12012-07-09 09:24:59 +010070
Brett Cannon15552c32016-07-08 10:46:21 -070071 When a virtual environment is active, any options that change the
72 installation path will be ignored from all distutils configuration files to
73 prevent projects being inadvertently installed outside of the virtual
74 environment.
Georg Brandl521ed522013-05-12 12:36:07 +020075
Brett Cannon15552c32016-07-08 10:46:21 -070076 When working in a command shell, users can make a virtual environment active
77 by running an ``activate`` script in the virtual environment's executables
78 directory (the precise filename is shell-dependent), which prepends the
79 virtual environment's directory for executables to the ``PATH`` environment
80 variable for the running shell. There should be no need in other
81 circumstances to activate a virtual environment—scripts installed into
82 virtual environments have a "shebang" line which points to the virtual
83 environment's Python interpreter. This means that the script will run with
84 that interpreter regardless of the value of ``PATH``. On Windows, "shebang"
85 line processing is supported if you have the Python Launcher for Windows
86 installed (this was added to Python in 3.3 - see :pep:`397` for more
87 details). Thus, double-clicking an installed script in a Windows Explorer
88 window should run the script with the correct interpreter without there
89 needing to be any reference to its virtual environment in ``PATH``.
Vinay Sajipa7045822013-09-06 09:50:43 +010090
Vinay Sajip7ded1f02012-05-26 03:45:29 +010091
Larry Hastings3732ed22014-03-15 21:13:56 -070092.. _venv-api:
93
Vinay Sajip7ded1f02012-05-26 03:45:29 +010094API
95---
96
Vinay Sajip7ded1f02012-05-26 03:45:29 +010097.. highlight:: python
98
Georg Brandldbab58f2012-06-24 16:37:59 +020099The high-level method described above makes use of a simple API which provides
100mechanisms for third-party virtual environment creators to customize environment
101creation according to their needs, the :class:`EnvBuilder` class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100102
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000103.. class:: EnvBuilder(system_site_packages=False, clear=False, \
Vinay Sajipfd0f84b2016-08-06 10:43:44 +0100104 symlinks=False, upgrade=False, with_pip=False, \
105 prompt=None)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100106
Georg Brandldbab58f2012-06-24 16:37:59 +0200107 The :class:`EnvBuilder` class accepts the following keyword arguments on
108 instantiation:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100109
Georg Brandldbab58f2012-06-24 16:37:59 +0200110 * ``system_site_packages`` -- a Boolean value indicating that the system Python
111 site-packages should be available to the environment (defaults to ``False``).
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100112
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200113 * ``clear`` -- a Boolean value which, if true, will delete the contents of
Vinay Sajipbd40d3e2012-10-11 17:22:45 +0100114 any existing target directory, before creating the environment.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100115
Georg Brandldbab58f2012-06-24 16:37:59 +0200116 * ``symlinks`` -- a Boolean value indicating whether to attempt to symlink the
117 Python binary (and any necessary DLLs or other binaries,
Miss Islington (bot)283e12f2018-06-01 10:57:12 -0700118 e.g. ``pythonw.exe``), rather than copying.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100119
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200120 * ``upgrade`` -- a Boolean value which, if true, will upgrade an existing
Vinay Sajipa945ad12012-07-09 09:24:59 +0100121 environment with the running Python - for use when that Python has been
122 upgraded in-place (defaults to ``False``).
123
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200124 * ``with_pip`` -- a Boolean value which, if true, ensures pip is
Nick Coghlanaa029da2014-02-09 10:10:24 +1000125 installed in the virtual environment. This uses :mod:`ensurepip` with
126 the ``--default-pip`` option.
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000127
Vinay Sajipfd0f84b2016-08-06 10:43:44 +0100128 * ``prompt`` -- a String to be used after virtual environment is activated
129 (defaults to ``None`` which means directory name of the environment would
130 be used).
131
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000132 .. versionchanged:: 3.4
133 Added the ``with_pip`` parameter
134
Vinay Sajipfd0f84b2016-08-06 10:43:44 +0100135 .. versionadded:: 3.6
136 Added the ``prompt`` parameter
137
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100138
Georg Brandldbab58f2012-06-24 16:37:59 +0200139 Creators of third-party virtual environment tools will be free to use the
140 provided ``EnvBuilder`` class as a base class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100141
Georg Brandldbab58f2012-06-24 16:37:59 +0200142 The returned env-builder is an object which has a method, ``create``:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100143
Georg Brandldbab58f2012-06-24 16:37:59 +0200144 .. method:: create(env_dir)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100145
Georg Brandldbab58f2012-06-24 16:37:59 +0200146 This method takes as required argument the path (absolute or relative to
147 the current directory) of the target directory which is to contain the
148 virtual environment. The ``create`` method will either create the
149 environment in the specified directory, or raise an appropriate
150 exception.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100151
Georg Brandldbab58f2012-06-24 16:37:59 +0200152 The ``create`` method of the ``EnvBuilder`` class illustrates the hooks
153 available for subclass customization::
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100154
Georg Brandldbab58f2012-06-24 16:37:59 +0200155 def create(self, env_dir):
156 """
157 Create a virtualized Python environment in a directory.
158 env_dir is the target directory to create an environment in.
159 """
160 env_dir = os.path.abspath(env_dir)
Georg Brandla0b79232013-10-06 12:52:49 +0200161 context = self.ensure_directories(env_dir)
Georg Brandldbab58f2012-06-24 16:37:59 +0200162 self.create_configuration(context)
163 self.setup_python(context)
164 self.setup_scripts(context)
165 self.post_setup(context)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100166
Georg Brandla0b79232013-10-06 12:52:49 +0200167 Each of the methods :meth:`ensure_directories`,
Georg Brandldbab58f2012-06-24 16:37:59 +0200168 :meth:`create_configuration`, :meth:`setup_python`,
169 :meth:`setup_scripts` and :meth:`post_setup` can be overridden.
170
Vinay Sajip577d4ff2013-07-12 21:52:51 +0100171 .. method:: ensure_directories(env_dir)
Georg Brandldbab58f2012-06-24 16:37:59 +0200172
173 Creates the environment directory and all necessary directories, and
174 returns a context object. This is just a holder for attributes (such as
Vinay Sajip577d4ff2013-07-12 21:52:51 +0100175 paths), for use by the other methods. The directories are allowed to
176 exist already, as long as either ``clear`` or ``upgrade`` were
177 specified to allow operating on an existing environment directory.
Georg Brandldbab58f2012-06-24 16:37:59 +0200178
179 .. method:: create_configuration(context)
180
181 Creates the ``pyvenv.cfg`` configuration file in the environment.
182
183 .. method:: setup_python(context)
184
185 Creates a copy of the Python executable (and, under Windows, DLLs) in
Vinay Sajip577d4ff2013-07-12 21:52:51 +0100186 the environment. On a POSIX system, if a specific executable
187 ``python3.x`` was used, symlinks to ``python`` and ``python3`` will be
188 created pointing to that executable, unless files with those names
189 already exist.
Georg Brandldbab58f2012-06-24 16:37:59 +0200190
191 .. method:: setup_scripts(context)
192
193 Installs activation scripts appropriate to the platform into the virtual
194 environment.
195
196 .. method:: post_setup(context)
197
198 A placeholder method which can be overridden in third party
199 implementations to pre-install packages in the virtual environment or
200 perform other post-creation steps.
201
202 In addition, :class:`EnvBuilder` provides this utility method that can be
203 called from :meth:`setup_scripts` or :meth:`post_setup` in subclasses to
204 assist in installing custom scripts into the virtual environment.
205
206 .. method:: install_scripts(context, path)
207
208 *path* is the path to a directory that should contain subdirectories
209 "common", "posix", "nt", each containing scripts destined for the bin
210 directory in the environment. The contents of "common" and the
211 directory corresponding to :data:`os.name` are copied after some text
212 replacement of placeholders:
213
214 * ``__VENV_DIR__`` is replaced with the absolute path of the environment
215 directory.
216
217 * ``__VENV_NAME__`` is replaced with the environment name (final path
218 segment of environment directory).
219
Vinay Sajipdff9e252013-10-02 11:36:16 +0100220 * ``__VENV_PROMPT__`` is replaced with the prompt (the environment
221 name surrounded by parentheses and with a following space)
222
Georg Brandldbab58f2012-06-24 16:37:59 +0200223 * ``__VENV_BIN_NAME__`` is replaced with the name of the bin directory
224 (either ``bin`` or ``Scripts``).
225
226 * ``__VENV_PYTHON__`` is replaced with the absolute path of the
227 environment's executable.
228
Vinay Sajip577d4ff2013-07-12 21:52:51 +0100229 The directories are allowed to exist (for when an existing environment
230 is being upgraded).
Georg Brandldbab58f2012-06-24 16:37:59 +0200231
232There is also a module-level convenience function:
233
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000234.. function:: create(env_dir, system_site_packages=False, clear=False, \
235 symlinks=False, with_pip=False)
Georg Brandldbab58f2012-06-24 16:37:59 +0200236
237 Create an :class:`EnvBuilder` with the given keyword arguments, and call its
238 :meth:`~EnvBuilder.create` method with the *env_dir* argument.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000239
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000240 .. versionchanged:: 3.4
241 Added the ``with_pip`` parameter
242
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000243An example of extending ``EnvBuilder``
244--------------------------------------
245
246The following script shows how to extend :class:`EnvBuilder` by implementing a
Brett Cannon15552c32016-07-08 10:46:21 -0700247subclass which installs setuptools and pip into a created virtual environment::
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000248
249 import os
250 import os.path
251 from subprocess import Popen, PIPE
252 import sys
253 from threading import Thread
254 from urllib.parse import urlparse
255 from urllib.request import urlretrieve
256 import venv
257
Vinay Sajip3c557f22013-07-12 20:54:25 +0100258 class ExtendedEnvBuilder(venv.EnvBuilder):
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000259 """
Vinay Sajip3c557f22013-07-12 20:54:25 +0100260 This builder installs setuptools and pip so that you can pip or
Brett Cannon15552c32016-07-08 10:46:21 -0700261 easy_install other packages into the created virtual environment.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000262
Vinay Sajip3c557f22013-07-12 20:54:25 +0100263 :param nodist: If True, setuptools and pip are not installed into the
Brett Cannon15552c32016-07-08 10:46:21 -0700264 created virtual environment.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000265 :param nopip: If True, pip is not installed into the created
Brett Cannon15552c32016-07-08 10:46:21 -0700266 virtual environment.
Vinay Sajip3c557f22013-07-12 20:54:25 +0100267 :param progress: If setuptools or pip are installed, the progress of the
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000268 installation can be monitored by passing a progress
269 callable. If specified, it is called with two
270 arguments: a string indicating some progress, and a
271 context indicating where the string is coming from.
272 The context argument can have one of three values:
273 'main', indicating that it is called from virtualize()
274 itself, and 'stdout' and 'stderr', which are obtained
275 by reading lines from the output streams of a subprocess
276 which is used to install the app.
277
278 If a callable is not specified, default progress
279 information is output to sys.stderr.
280 """
281
282 def __init__(self, *args, **kwargs):
283 self.nodist = kwargs.pop('nodist', False)
284 self.nopip = kwargs.pop('nopip', False)
285 self.progress = kwargs.pop('progress', None)
286 self.verbose = kwargs.pop('verbose', False)
287 super().__init__(*args, **kwargs)
288
289 def post_setup(self, context):
290 """
291 Set up any packages which need to be pre-installed into the
Brett Cannon15552c32016-07-08 10:46:21 -0700292 virtual environment being created.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000293
Brett Cannon15552c32016-07-08 10:46:21 -0700294 :param context: The information for the virtual environment
295 creation request being processed.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000296 """
Vinay Sajip3c557f22013-07-12 20:54:25 +0100297 os.environ['VIRTUAL_ENV'] = context.env_dir
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000298 if not self.nodist:
Vinay Sajip3c557f22013-07-12 20:54:25 +0100299 self.install_setuptools(context)
300 # Can't install pip without setuptools
301 if not self.nopip and not self.nodist:
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000302 self.install_pip(context)
303
304 def reader(self, stream, context):
305 """
306 Read lines from a subprocess' output stream and either pass to a progress
307 callable (if specified) or write progress information to sys.stderr.
308 """
309 progress = self.progress
310 while True:
311 s = stream.readline()
312 if not s:
313 break
314 if progress is not None:
315 progress(s, context)
316 else:
317 if not self.verbose:
318 sys.stderr.write('.')
319 else:
320 sys.stderr.write(s.decode('utf-8'))
321 sys.stderr.flush()
Vinay Sajipad6bb032013-07-12 21:44:35 +0100322 stream.close()
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000323
324 def install_script(self, context, name, url):
325 _, _, path, _, _, _ = urlparse(url)
326 fn = os.path.split(path)[-1]
327 binpath = context.bin_path
328 distpath = os.path.join(binpath, fn)
Brett Cannon15552c32016-07-08 10:46:21 -0700329 # Download script into the virtual environment's binaries folder
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000330 urlretrieve(url, distpath)
331 progress = self.progress
Vinay Sajip3c557f22013-07-12 20:54:25 +0100332 if self.verbose:
333 term = '\n'
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000334 else:
Vinay Sajip3c557f22013-07-12 20:54:25 +0100335 term = ''
336 if progress is not None:
337 progress('Installing %s ...%s' % (name, term), 'main')
338 else:
339 sys.stderr.write('Installing %s ...%s' % (name, term))
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000340 sys.stderr.flush()
Brett Cannon15552c32016-07-08 10:46:21 -0700341 # Install in the virtual environment
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000342 args = [context.env_exe, fn]
343 p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=binpath)
344 t1 = Thread(target=self.reader, args=(p.stdout, 'stdout'))
345 t1.start()
346 t2 = Thread(target=self.reader, args=(p.stderr, 'stderr'))
347 t2.start()
348 p.wait()
349 t1.join()
350 t2.join()
351 if progress is not None:
352 progress('done.', 'main')
353 else:
354 sys.stderr.write('done.\n')
355 # Clean up - no longer needed
356 os.unlink(distpath)
357
Vinay Sajip3c557f22013-07-12 20:54:25 +0100358 def install_setuptools(self, context):
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000359 """
Brett Cannon15552c32016-07-08 10:46:21 -0700360 Install setuptools in the virtual environment.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000361
Brett Cannon15552c32016-07-08 10:46:21 -0700362 :param context: The information for the virtual environment
363 creation request being processed.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000364 """
Vinay Sajip3c557f22013-07-12 20:54:25 +0100365 url = 'https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py'
366 self.install_script(context, 'setuptools', url)
367 # clear up the setuptools archive which gets downloaded
368 pred = lambda o: o.startswith('setuptools-') and o.endswith('.tar.gz')
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000369 files = filter(pred, os.listdir(context.bin_path))
370 for f in files:
371 f = os.path.join(context.bin_path, f)
372 os.unlink(f)
373
374 def install_pip(self, context):
375 """
Brett Cannon15552c32016-07-08 10:46:21 -0700376 Install pip in the virtual environment.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000377
Brett Cannon15552c32016-07-08 10:46:21 -0700378 :param context: The information for the virtual environment
379 creation request being processed.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000380 """
381 url = 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py'
382 self.install_script(context, 'pip', url)
383
384 def main(args=None):
385 compatible = True
386 if sys.version_info < (3, 3):
387 compatible = False
388 elif not hasattr(sys, 'base_prefix'):
389 compatible = False
390 if not compatible:
391 raise ValueError('This script is only for use with '
392 'Python 3.3 or later')
393 else:
394 import argparse
395
396 parser = argparse.ArgumentParser(prog=__name__,
397 description='Creates virtual Python '
398 'environments in one or '
399 'more target '
400 'directories.')
401 parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
Brett Cannon15552c32016-07-08 10:46:21 -0700402 help='A directory in which to create the
403 'virtual environment.')
Vinay Sajip3c557f22013-07-12 20:54:25 +0100404 parser.add_argument('--no-setuptools', default=False,
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000405 action='store_true', dest='nodist',
Vinay Sajip3c557f22013-07-12 20:54:25 +0100406 help="Don't install setuptools or pip in the "
407 "virtual environment.")
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000408 parser.add_argument('--no-pip', default=False,
409 action='store_true', dest='nopip',
410 help="Don't install pip in the virtual "
411 "environment.")
412 parser.add_argument('--system-site-packages', default=False,
413 action='store_true', dest='system_site',
414 help='Give the virtual environment access to the '
415 'system site-packages dir.')
416 if os.name == 'nt':
417 use_symlinks = False
418 else:
419 use_symlinks = True
420 parser.add_argument('--symlinks', default=use_symlinks,
421 action='store_true', dest='symlinks',
422 help='Try to use symlinks rather than copies, '
423 'when symlinks are not the default for '
424 'the platform.')
425 parser.add_argument('--clear', default=False, action='store_true',
426 dest='clear', help='Delete the contents of the '
Brett Cannon15552c32016-07-08 10:46:21 -0700427 'virtual environment '
428 'directory if it already '
429 'exists, before virtual '
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000430 'environment creation.')
431 parser.add_argument('--upgrade', default=False, action='store_true',
Brett Cannon15552c32016-07-08 10:46:21 -0700432 dest='upgrade', help='Upgrade the virtual '
433 'environment directory to '
434 'use this version of '
435 'Python, assuming Python '
436 'has been upgraded '
437 'in-place.')
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000438 parser.add_argument('--verbose', default=False, action='store_true',
439 dest='verbose', help='Display the output '
440 'from the scripts which '
Vinay Sajip3c557f22013-07-12 20:54:25 +0100441 'install setuptools and pip.')
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000442 options = parser.parse_args(args)
443 if options.upgrade and options.clear:
444 raise ValueError('you cannot supply --upgrade and --clear together.')
Vinay Sajip3c557f22013-07-12 20:54:25 +0100445 builder = ExtendedEnvBuilder(system_site_packages=options.system_site,
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000446 clear=options.clear,
447 symlinks=options.symlinks,
448 upgrade=options.upgrade,
449 nodist=options.nodist,
450 nopip=options.nopip,
451 verbose=options.verbose)
452 for d in options.dirs:
453 builder.create(d)
454
455 if __name__ == '__main__':
456 rc = 1
457 try:
458 main()
459 rc = 0
460 except Exception as e:
461 print('Error: %s' % e, file=sys.stderr)
462 sys.exit(rc)
463
Vinay Sajip3c557f22013-07-12 20:54:25 +0100464
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000465This script is also available for download `online
Sanyam Khurana338cd832018-01-20 05:55:37 +0530466<https://gist.github.com/vsajip/4673395>`_.