blob: 412808ad44866d0884bda4ec23e19f09b9c2999f [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
Elena Oat50326922018-07-28 05:58:05 -070020site 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
Elena Oat50326922018-07-28 05:58:05 -070027.. seealso::
28
29 `Python Packaging User Guide: Creating and using virtual environments
30 <https://packaging.python.org/installing/#creating-virtual-environments>`__
Brett Cannonf8c15052016-10-21 12:53:40 -070031
32
Vinay Sajip7ded1f02012-05-26 03:45:29 +010033Creating virtual environments
34-----------------------------
35
Vinay Sajipc4618e32012-07-10 08:21:07 +010036.. include:: /using/venv-create.inc
Vinay Sajip7ded1f02012-05-26 03:45:29 +010037
Vinay Sajipa945ad12012-07-09 09:24:59 +010038
Vinay Sajipcd9b7462012-07-09 10:37:01 +010039.. _venv-def:
40
Brett Cannon15552c32016-07-08 10:46:21 -070041.. note:: A virtual environment is a Python environment such that the Python
42 interpreter, libraries and scripts installed into it are isolated from those
43 installed in other virtual environments, and (by default) any libraries
44 installed in a "system" Python, i.e., one which is installed as part of your
45 operating system.
Vinay Sajipa945ad12012-07-09 09:24:59 +010046
Brett Cannon15552c32016-07-08 10:46:21 -070047 A virtual environment is a directory tree which contains Python executable
48 files and other files which indicate that it is a virtual environment.
Vinay Sajipa945ad12012-07-09 09:24:59 +010049
Jason R. Coombs13266fb2014-05-12 22:40:49 -040050 Common installation tools such as ``Setuptools`` and ``pip`` work as
Brett Cannon15552c32016-07-08 10:46:21 -070051 expected with virtual environments. In other words, when a virtual
52 environment is active, they install Python packages into the virtual
53 environment without needing to be told to do so explicitly.
Vinay Sajipa945ad12012-07-09 09:24:59 +010054
Brett Cannon15552c32016-07-08 10:46:21 -070055 When a virtual environment is active (i.e., the virtual environment's Python
56 interpreter is running), the attributes :attr:`sys.prefix` and
57 :attr:`sys.exec_prefix` point to the base directory of the virtual
58 environment, whereas :attr:`sys.base_prefix` and
59 :attr:`sys.base_exec_prefix` point to the non-virtual environment Python
60 installation which was used to create the virtual environment. If a virtual
61 environment is not active, then :attr:`sys.prefix` is the same as
62 :attr:`sys.base_prefix` and :attr:`sys.exec_prefix` is the same as
63 :attr:`sys.base_exec_prefix` (they all point to a non-virtual environment
64 Python installation).
Vinay Sajipa945ad12012-07-09 09:24:59 +010065
Brett Cannon15552c32016-07-08 10:46:21 -070066 When a virtual environment is active, any options that change the
67 installation path will be ignored from all distutils configuration files to
68 prevent projects being inadvertently installed outside of the virtual
69 environment.
Georg Brandl521ed522013-05-12 12:36:07 +020070
Brett Cannon15552c32016-07-08 10:46:21 -070071 When working in a command shell, users can make a virtual environment active
72 by running an ``activate`` script in the virtual environment's executables
73 directory (the precise filename is shell-dependent), which prepends the
74 virtual environment's directory for executables to the ``PATH`` environment
75 variable for the running shell. There should be no need in other
76 circumstances to activate a virtual environment—scripts installed into
77 virtual environments have a "shebang" line which points to the virtual
78 environment's Python interpreter. This means that the script will run with
79 that interpreter regardless of the value of ``PATH``. On Windows, "shebang"
80 line processing is supported if you have the Python Launcher for Windows
81 installed (this was added to Python in 3.3 - see :pep:`397` for more
82 details). Thus, double-clicking an installed script in a Windows Explorer
83 window should run the script with the correct interpreter without there
84 needing to be any reference to its virtual environment in ``PATH``.
Vinay Sajipa7045822013-09-06 09:50:43 +010085
Vinay Sajip7ded1f02012-05-26 03:45:29 +010086
Larry Hastings3732ed22014-03-15 21:13:56 -070087.. _venv-api:
88
Vinay Sajip7ded1f02012-05-26 03:45:29 +010089API
90---
91
Vinay Sajip7ded1f02012-05-26 03:45:29 +010092.. highlight:: python
93
Georg Brandldbab58f2012-06-24 16:37:59 +020094The high-level method described above makes use of a simple API which provides
95mechanisms for third-party virtual environment creators to customize environment
96creation according to their needs, the :class:`EnvBuilder` class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +010097
Nick Coghlan8fbdb092013-11-23 00:30:34 +100098.. class:: EnvBuilder(system_site_packages=False, clear=False, \
Vinay Sajipfd0f84b2016-08-06 10:43:44 +010099 symlinks=False, upgrade=False, with_pip=False, \
100 prompt=None)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100101
Georg Brandldbab58f2012-06-24 16:37:59 +0200102 The :class:`EnvBuilder` class accepts the following keyword arguments on
103 instantiation:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100104
Georg Brandldbab58f2012-06-24 16:37:59 +0200105 * ``system_site_packages`` -- a Boolean value indicating that the system Python
106 site-packages should be available to the environment (defaults to ``False``).
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100107
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200108 * ``clear`` -- a Boolean value which, if true, will delete the contents of
Vinay Sajipbd40d3e2012-10-11 17:22:45 +0100109 any existing target directory, before creating the environment.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100110
Georg Brandldbab58f2012-06-24 16:37:59 +0200111 * ``symlinks`` -- a Boolean value indicating whether to attempt to symlink the
Steve Dowera1f9a332019-01-30 13:49:14 -0800112 Python binary rather than copying.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100113
Serhiy Storchakafbc1c262013-11-29 12:17:13 +0200114 * ``upgrade`` -- a Boolean value which, if true, will upgrade an existing
Vinay Sajipa945ad12012-07-09 09:24:59 +0100115 environment with the running Python - for use when that Python has been
116 upgraded in-place (defaults to ``False``).
117
Serhiy Storchaka0e90e992013-11-29 12:19:53 +0200118 * ``with_pip`` -- a Boolean value which, if true, ensures pip is
Nick Coghlanaa029da2014-02-09 10:10:24 +1000119 installed in the virtual environment. This uses :mod:`ensurepip` with
120 the ``--default-pip`` option.
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000121
Vinay Sajipfd0f84b2016-08-06 10:43:44 +0100122 * ``prompt`` -- a String to be used after virtual environment is activated
123 (defaults to ``None`` which means directory name of the environment would
124 be used).
125
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000126 .. versionchanged:: 3.4
127 Added the ``with_pip`` parameter
128
Vinay Sajipfd0f84b2016-08-06 10:43:44 +0100129 .. versionadded:: 3.6
130 Added the ``prompt`` parameter
131
Georg Brandldbab58f2012-06-24 16:37:59 +0200132 Creators of third-party virtual environment tools will be free to use the
133 provided ``EnvBuilder`` class as a base class.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100134
Georg Brandldbab58f2012-06-24 16:37:59 +0200135 The returned env-builder is an object which has a method, ``create``:
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100136
Georg Brandldbab58f2012-06-24 16:37:59 +0200137 .. method:: create(env_dir)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100138
Georg Brandldbab58f2012-06-24 16:37:59 +0200139 This method takes as required argument the path (absolute or relative to
140 the current directory) of the target directory which is to contain the
141 virtual environment. The ``create`` method will either create the
142 environment in the specified directory, or raise an appropriate
143 exception.
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100144
Georg Brandldbab58f2012-06-24 16:37:59 +0200145 The ``create`` method of the ``EnvBuilder`` class illustrates the hooks
146 available for subclass customization::
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100147
Georg Brandldbab58f2012-06-24 16:37:59 +0200148 def create(self, env_dir):
149 """
150 Create a virtualized Python environment in a directory.
151 env_dir is the target directory to create an environment in.
152 """
153 env_dir = os.path.abspath(env_dir)
Georg Brandla0b79232013-10-06 12:52:49 +0200154 context = self.ensure_directories(env_dir)
Georg Brandldbab58f2012-06-24 16:37:59 +0200155 self.create_configuration(context)
156 self.setup_python(context)
157 self.setup_scripts(context)
158 self.post_setup(context)
Vinay Sajip7ded1f02012-05-26 03:45:29 +0100159
Georg Brandla0b79232013-10-06 12:52:49 +0200160 Each of the methods :meth:`ensure_directories`,
Georg Brandldbab58f2012-06-24 16:37:59 +0200161 :meth:`create_configuration`, :meth:`setup_python`,
162 :meth:`setup_scripts` and :meth:`post_setup` can be overridden.
163
Vinay Sajip577d4ff2013-07-12 21:52:51 +0100164 .. method:: ensure_directories(env_dir)
Georg Brandldbab58f2012-06-24 16:37:59 +0200165
166 Creates the environment directory and all necessary directories, and
167 returns a context object. This is just a holder for attributes (such as
Vinay Sajip577d4ff2013-07-12 21:52:51 +0100168 paths), for use by the other methods. The directories are allowed to
169 exist already, as long as either ``clear`` or ``upgrade`` were
170 specified to allow operating on an existing environment directory.
Georg Brandldbab58f2012-06-24 16:37:59 +0200171
172 .. method:: create_configuration(context)
173
174 Creates the ``pyvenv.cfg`` configuration file in the environment.
175
176 .. method:: setup_python(context)
177
Steve Dowera1f9a332019-01-30 13:49:14 -0800178 Creates a copy or symlink to the Python executable in the environment.
179 On POSIX systems, if a specific executable ``python3.x`` was used,
180 symlinks to ``python`` and ``python3`` will be created pointing to that
181 executable, unless files with those names already exist.
Georg Brandldbab58f2012-06-24 16:37:59 +0200182
183 .. method:: setup_scripts(context)
184
185 Installs activation scripts appropriate to the platform into the virtual
Steve Dowera1f9a332019-01-30 13:49:14 -0800186 environment.
Georg Brandldbab58f2012-06-24 16:37:59 +0200187
188 .. method:: post_setup(context)
189
190 A placeholder method which can be overridden in third party
191 implementations to pre-install packages in the virtual environment or
192 perform other post-creation steps.
193
Steve Dower1c3de542018-12-10 08:11:21 -0800194 .. versionchanged:: 3.7.2
195 Windows now uses redirector scripts for ``python[w].exe`` instead of
Steve Dowera1f9a332019-01-30 13:49:14 -0800196 copying the actual binaries. In 3.7.2 only :meth:`setup_python` does
197 nothing unless running from a build in the source tree.
198
199 .. versionchanged:: 3.7.3
200 Windows copies the redirector scripts as part of :meth:`setup_python`
201 instead of :meth:`setup_scripts`. This was not the case in 3.7.2.
202 When using symlinks, the original executables will be linked.
Steve Dower1c3de542018-12-10 08:11:21 -0800203
Georg Brandldbab58f2012-06-24 16:37:59 +0200204 In addition, :class:`EnvBuilder` provides this utility method that can be
205 called from :meth:`setup_scripts` or :meth:`post_setup` in subclasses to
206 assist in installing custom scripts into the virtual environment.
207
208 .. method:: install_scripts(context, path)
209
210 *path* is the path to a directory that should contain subdirectories
211 "common", "posix", "nt", each containing scripts destined for the bin
212 directory in the environment. The contents of "common" and the
213 directory corresponding to :data:`os.name` are copied after some text
214 replacement of placeholders:
215
216 * ``__VENV_DIR__`` is replaced with the absolute path of the environment
217 directory.
218
219 * ``__VENV_NAME__`` is replaced with the environment name (final path
220 segment of environment directory).
221
Vinay Sajipdff9e252013-10-02 11:36:16 +0100222 * ``__VENV_PROMPT__`` is replaced with the prompt (the environment
223 name surrounded by parentheses and with a following space)
224
Georg Brandldbab58f2012-06-24 16:37:59 +0200225 * ``__VENV_BIN_NAME__`` is replaced with the name of the bin directory
226 (either ``bin`` or ``Scripts``).
227
228 * ``__VENV_PYTHON__`` is replaced with the absolute path of the
229 environment's executable.
230
Vinay Sajip577d4ff2013-07-12 21:52:51 +0100231 The directories are allowed to exist (for when an existing environment
232 is being upgraded).
Georg Brandldbab58f2012-06-24 16:37:59 +0200233
234There is also a module-level convenience function:
235
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000236.. function:: create(env_dir, system_site_packages=False, clear=False, \
237 symlinks=False, with_pip=False)
Georg Brandldbab58f2012-06-24 16:37:59 +0200238
239 Create an :class:`EnvBuilder` with the given keyword arguments, and call its
240 :meth:`~EnvBuilder.create` method with the *env_dir* argument.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000241
Nick Coghlan8fbdb092013-11-23 00:30:34 +1000242 .. versionchanged:: 3.4
243 Added the ``with_pip`` parameter
244
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000245An example of extending ``EnvBuilder``
246--------------------------------------
247
248The following script shows how to extend :class:`EnvBuilder` by implementing a
Brett Cannon15552c32016-07-08 10:46:21 -0700249subclass which installs setuptools and pip into a created virtual environment::
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000250
251 import os
252 import os.path
253 from subprocess import Popen, PIPE
254 import sys
255 from threading import Thread
256 from urllib.parse import urlparse
257 from urllib.request import urlretrieve
258 import venv
259
Vinay Sajip3c557f22013-07-12 20:54:25 +0100260 class ExtendedEnvBuilder(venv.EnvBuilder):
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000261 """
Vinay Sajip3c557f22013-07-12 20:54:25 +0100262 This builder installs setuptools and pip so that you can pip or
Brett Cannon15552c32016-07-08 10:46:21 -0700263 easy_install other packages into the created virtual environment.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000264
Vinay Sajip3c557f22013-07-12 20:54:25 +0100265 :param nodist: If True, setuptools and pip are not installed into the
Brett Cannon15552c32016-07-08 10:46:21 -0700266 created virtual environment.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000267 :param nopip: If True, pip is not installed into the created
Brett Cannon15552c32016-07-08 10:46:21 -0700268 virtual environment.
Vinay Sajip3c557f22013-07-12 20:54:25 +0100269 :param progress: If setuptools or pip are installed, the progress of the
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000270 installation can be monitored by passing a progress
271 callable. If specified, it is called with two
272 arguments: a string indicating some progress, and a
273 context indicating where the string is coming from.
274 The context argument can have one of three values:
275 'main', indicating that it is called from virtualize()
276 itself, and 'stdout' and 'stderr', which are obtained
277 by reading lines from the output streams of a subprocess
278 which is used to install the app.
279
280 If a callable is not specified, default progress
281 information is output to sys.stderr.
282 """
283
284 def __init__(self, *args, **kwargs):
285 self.nodist = kwargs.pop('nodist', False)
286 self.nopip = kwargs.pop('nopip', False)
287 self.progress = kwargs.pop('progress', None)
288 self.verbose = kwargs.pop('verbose', False)
289 super().__init__(*args, **kwargs)
290
291 def post_setup(self, context):
292 """
293 Set up any packages which need to be pre-installed into the
Brett Cannon15552c32016-07-08 10:46:21 -0700294 virtual environment being created.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000295
Brett Cannon15552c32016-07-08 10:46:21 -0700296 :param context: The information for the virtual environment
297 creation request being processed.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000298 """
Vinay Sajip3c557f22013-07-12 20:54:25 +0100299 os.environ['VIRTUAL_ENV'] = context.env_dir
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000300 if not self.nodist:
Vinay Sajip3c557f22013-07-12 20:54:25 +0100301 self.install_setuptools(context)
302 # Can't install pip without setuptools
303 if not self.nopip and not self.nodist:
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000304 self.install_pip(context)
305
306 def reader(self, stream, context):
307 """
308 Read lines from a subprocess' output stream and either pass to a progress
309 callable (if specified) or write progress information to sys.stderr.
310 """
311 progress = self.progress
312 while True:
313 s = stream.readline()
314 if not s:
315 break
316 if progress is not None:
317 progress(s, context)
318 else:
319 if not self.verbose:
320 sys.stderr.write('.')
321 else:
322 sys.stderr.write(s.decode('utf-8'))
323 sys.stderr.flush()
Vinay Sajipad6bb032013-07-12 21:44:35 +0100324 stream.close()
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000325
326 def install_script(self, context, name, url):
327 _, _, path, _, _, _ = urlparse(url)
328 fn = os.path.split(path)[-1]
329 binpath = context.bin_path
330 distpath = os.path.join(binpath, fn)
Brett Cannon15552c32016-07-08 10:46:21 -0700331 # Download script into the virtual environment's binaries folder
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000332 urlretrieve(url, distpath)
333 progress = self.progress
Vinay Sajip3c557f22013-07-12 20:54:25 +0100334 if self.verbose:
335 term = '\n'
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000336 else:
Vinay Sajip3c557f22013-07-12 20:54:25 +0100337 term = ''
338 if progress is not None:
339 progress('Installing %s ...%s' % (name, term), 'main')
340 else:
341 sys.stderr.write('Installing %s ...%s' % (name, term))
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000342 sys.stderr.flush()
Brett Cannon15552c32016-07-08 10:46:21 -0700343 # Install in the virtual environment
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000344 args = [context.env_exe, fn]
345 p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=binpath)
346 t1 = Thread(target=self.reader, args=(p.stdout, 'stdout'))
347 t1.start()
348 t2 = Thread(target=self.reader, args=(p.stderr, 'stderr'))
349 t2.start()
350 p.wait()
351 t1.join()
352 t2.join()
353 if progress is not None:
354 progress('done.', 'main')
355 else:
356 sys.stderr.write('done.\n')
357 # Clean up - no longer needed
358 os.unlink(distpath)
359
Vinay Sajip3c557f22013-07-12 20:54:25 +0100360 def install_setuptools(self, context):
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000361 """
Brett Cannon15552c32016-07-08 10:46:21 -0700362 Install setuptools in the virtual environment.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000363
Brett Cannon15552c32016-07-08 10:46:21 -0700364 :param context: The information for the virtual environment
365 creation request being processed.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000366 """
Vinay Sajip3c557f22013-07-12 20:54:25 +0100367 url = 'https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py'
368 self.install_script(context, 'setuptools', url)
369 # clear up the setuptools archive which gets downloaded
370 pred = lambda o: o.startswith('setuptools-') and o.endswith('.tar.gz')
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000371 files = filter(pred, os.listdir(context.bin_path))
372 for f in files:
373 f = os.path.join(context.bin_path, f)
374 os.unlink(f)
375
376 def install_pip(self, context):
377 """
Brett Cannon15552c32016-07-08 10:46:21 -0700378 Install pip in the virtual environment.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000379
Brett Cannon15552c32016-07-08 10:46:21 -0700380 :param context: The information for the virtual environment
381 creation request being processed.
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000382 """
383 url = 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py'
384 self.install_script(context, 'pip', url)
385
386 def main(args=None):
387 compatible = True
388 if sys.version_info < (3, 3):
389 compatible = False
390 elif not hasattr(sys, 'base_prefix'):
391 compatible = False
392 if not compatible:
393 raise ValueError('This script is only for use with '
394 'Python 3.3 or later')
395 else:
396 import argparse
397
398 parser = argparse.ArgumentParser(prog=__name__,
399 description='Creates virtual Python '
400 'environments in one or '
401 'more target '
402 'directories.')
403 parser.add_argument('dirs', metavar='ENV_DIR', nargs='+',
Brett Cannon15552c32016-07-08 10:46:21 -0700404 help='A directory in which to create the
405 'virtual environment.')
Vinay Sajip3c557f22013-07-12 20:54:25 +0100406 parser.add_argument('--no-setuptools', default=False,
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000407 action='store_true', dest='nodist',
Vinay Sajip3c557f22013-07-12 20:54:25 +0100408 help="Don't install setuptools or pip in the "
409 "virtual environment.")
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000410 parser.add_argument('--no-pip', default=False,
411 action='store_true', dest='nopip',
412 help="Don't install pip in the virtual "
413 "environment.")
414 parser.add_argument('--system-site-packages', default=False,
415 action='store_true', dest='system_site',
416 help='Give the virtual environment access to the '
417 'system site-packages dir.')
418 if os.name == 'nt':
419 use_symlinks = False
420 else:
421 use_symlinks = True
422 parser.add_argument('--symlinks', default=use_symlinks,
423 action='store_true', dest='symlinks',
424 help='Try to use symlinks rather than copies, '
425 'when symlinks are not the default for '
426 'the platform.')
427 parser.add_argument('--clear', default=False, action='store_true',
428 dest='clear', help='Delete the contents of the '
Brett Cannon15552c32016-07-08 10:46:21 -0700429 'virtual environment '
430 'directory if it already '
431 'exists, before virtual '
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000432 'environment creation.')
433 parser.add_argument('--upgrade', default=False, action='store_true',
Brett Cannon15552c32016-07-08 10:46:21 -0700434 dest='upgrade', help='Upgrade the virtual '
435 'environment directory to '
436 'use this version of '
437 'Python, assuming Python '
438 'has been upgraded '
439 'in-place.')
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000440 parser.add_argument('--verbose', default=False, action='store_true',
441 dest='verbose', help='Display the output '
442 'from the scripts which '
Vinay Sajip3c557f22013-07-12 20:54:25 +0100443 'install setuptools and pip.')
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000444 options = parser.parse_args(args)
445 if options.upgrade and options.clear:
446 raise ValueError('you cannot supply --upgrade and --clear together.')
Vinay Sajip3c557f22013-07-12 20:54:25 +0100447 builder = ExtendedEnvBuilder(system_site_packages=options.system_site,
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000448 clear=options.clear,
449 symlinks=options.symlinks,
450 upgrade=options.upgrade,
451 nodist=options.nodist,
452 nopip=options.nopip,
453 verbose=options.verbose)
454 for d in options.dirs:
455 builder.create(d)
456
457 if __name__ == '__main__':
458 rc = 1
459 try:
460 main()
461 rc = 0
462 except Exception as e:
463 print('Error: %s' % e, file=sys.stderr)
464 sys.exit(rc)
465
Vinay Sajip3c557f22013-07-12 20:54:25 +0100466
Vinay Sajip2b4fcfb2013-01-30 13:44:00 +0000467This script is also available for download `online
Sanyam Khurana338cd832018-01-20 05:55:37 +0530468<https://gist.github.com/vsajip/4673395>`_.