Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 1 | :mod:`venv` --- Creation of virtual environments |
| 2 | ================================================ |
| 3 | |
| 4 | .. module:: venv |
| 5 | :synopsis: Creation of virtual environments. |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 6 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 7 | .. moduleauthor:: Vinay Sajip <vinay_sajip@yahoo.co.uk> |
| 8 | .. sectionauthor:: Vinay Sajip <vinay_sajip@yahoo.co.uk> |
| 9 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 10 | .. versionadded:: 3.3 |
| 11 | |
Terry Jan Reedy | fa089b9 | 2016-06-11 15:02:54 -0400 | [diff] [blame] | 12 | **Source code:** :source:`Lib/venv/` |
| 13 | |
| 14 | .. index:: pair: Environments; virtual |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 15 | |
| 16 | -------------- |
| 17 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 18 | The :mod:`venv` module provides support for creating lightweight "virtual |
| 19 | environments" with their own site directories, optionally isolated from system |
| 20 | site directories. Each virtual environment has its own Python binary (allowing |
| 21 | creation of environments with various Python versions) and can have its own |
| 22 | independent set of installed Python packages in its site directories. |
| 23 | |
Vinay Sajip | a704582 | 2013-09-06 09:50:43 +0100 | [diff] [blame] | 24 | See :pep:`405` for more information about Python virtual environments. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 25 | |
| 26 | Creating virtual environments |
| 27 | ----------------------------- |
| 28 | |
Vinay Sajip | c4618e3 | 2012-07-10 08:21:07 +0100 | [diff] [blame] | 29 | .. include:: /using/venv-create.inc |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 30 | |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 31 | |
Vinay Sajip | cd9b746 | 2012-07-09 10:37:01 +0100 | [diff] [blame] | 32 | .. _venv-def: |
| 33 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 34 | .. note:: A virtual environment is a Python environment such that the Python |
| 35 | interpreter, libraries and scripts installed into it are isolated from those |
| 36 | installed in other virtual environments, and (by default) any libraries |
| 37 | installed in a "system" Python, i.e., one which is installed as part of your |
| 38 | operating system. |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 39 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 40 | A virtual environment is a directory tree which contains Python executable |
| 41 | files and other files which indicate that it is a virtual environment. |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 42 | |
Jason R. Coombs | 13266fb | 2014-05-12 22:40:49 -0400 | [diff] [blame] | 43 | Common installation tools such as ``Setuptools`` and ``pip`` work as |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 44 | expected with virtual environments. In other words, when a virtual |
| 45 | environment is active, they install Python packages into the virtual |
| 46 | environment without needing to be told to do so explicitly. |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 47 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 48 | When a virtual environment is active (i.e., the virtual environment's Python |
| 49 | interpreter is running), the attributes :attr:`sys.prefix` and |
| 50 | :attr:`sys.exec_prefix` point to the base directory of the virtual |
| 51 | environment, whereas :attr:`sys.base_prefix` and |
| 52 | :attr:`sys.base_exec_prefix` point to the non-virtual environment Python |
| 53 | installation which was used to create the virtual environment. If a virtual |
| 54 | environment is not active, then :attr:`sys.prefix` is the same as |
| 55 | :attr:`sys.base_prefix` and :attr:`sys.exec_prefix` is the same as |
| 56 | :attr:`sys.base_exec_prefix` (they all point to a non-virtual environment |
| 57 | Python installation). |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 58 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 59 | When a virtual environment is active, any options that change the |
| 60 | installation path will be ignored from all distutils configuration files to |
| 61 | prevent projects being inadvertently installed outside of the virtual |
| 62 | environment. |
Georg Brandl | 521ed52 | 2013-05-12 12:36:07 +0200 | [diff] [blame] | 63 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 64 | When working in a command shell, users can make a virtual environment active |
| 65 | by running an ``activate`` script in the virtual environment's executables |
| 66 | directory (the precise filename is shell-dependent), which prepends the |
| 67 | virtual environment's directory for executables to the ``PATH`` environment |
| 68 | variable for the running shell. There should be no need in other |
| 69 | circumstances to activate a virtual environment—scripts installed into |
| 70 | virtual environments have a "shebang" line which points to the virtual |
| 71 | environment's Python interpreter. This means that the script will run with |
| 72 | that interpreter regardless of the value of ``PATH``. On Windows, "shebang" |
| 73 | line processing is supported if you have the Python Launcher for Windows |
| 74 | installed (this was added to Python in 3.3 - see :pep:`397` for more |
| 75 | details). Thus, double-clicking an installed script in a Windows Explorer |
| 76 | window should run the script with the correct interpreter without there |
| 77 | needing to be any reference to its virtual environment in ``PATH``. |
Vinay Sajip | a704582 | 2013-09-06 09:50:43 +0100 | [diff] [blame] | 78 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 79 | |
Larry Hastings | 3732ed2 | 2014-03-15 21:13:56 -0700 | [diff] [blame] | 80 | .. _venv-api: |
| 81 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 82 | API |
| 83 | --- |
| 84 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 85 | .. highlight:: python |
| 86 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 87 | The high-level method described above makes use of a simple API which provides |
| 88 | mechanisms for third-party virtual environment creators to customize environment |
| 89 | creation according to their needs, the :class:`EnvBuilder` class. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 90 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 91 | .. class:: EnvBuilder(system_site_packages=False, clear=False, \ |
Vinay Sajip | fd0f84b | 2016-08-06 10:43:44 +0100 | [diff] [blame] | 92 | symlinks=False, upgrade=False, with_pip=False, \ |
| 93 | prompt=None) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 94 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 95 | The :class:`EnvBuilder` class accepts the following keyword arguments on |
| 96 | instantiation: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 97 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 98 | * ``system_site_packages`` -- a Boolean value indicating that the system Python |
| 99 | site-packages should be available to the environment (defaults to ``False``). |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 100 | |
Serhiy Storchaka | 0e90e99 | 2013-11-29 12:19:53 +0200 | [diff] [blame] | 101 | * ``clear`` -- a Boolean value which, if true, will delete the contents of |
Vinay Sajip | bd40d3e | 2012-10-11 17:22:45 +0100 | [diff] [blame] | 102 | any existing target directory, before creating the environment. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 103 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 104 | * ``symlinks`` -- a Boolean value indicating whether to attempt to symlink the |
| 105 | Python binary (and any necessary DLLs or other binaries, |
| 106 | e.g. ``pythonw.exe``), rather than copying. Defaults to ``True`` on Linux and |
Vinay Sajip | 90db661 | 2012-07-17 17:33:46 +0100 | [diff] [blame] | 107 | Unix systems, but ``False`` on Windows. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 108 | |
Serhiy Storchaka | fbc1c26 | 2013-11-29 12:17:13 +0200 | [diff] [blame] | 109 | * ``upgrade`` -- a Boolean value which, if true, will upgrade an existing |
Vinay Sajip | a945ad1 | 2012-07-09 09:24:59 +0100 | [diff] [blame] | 110 | environment with the running Python - for use when that Python has been |
| 111 | upgraded in-place (defaults to ``False``). |
| 112 | |
Serhiy Storchaka | 0e90e99 | 2013-11-29 12:19:53 +0200 | [diff] [blame] | 113 | * ``with_pip`` -- a Boolean value which, if true, ensures pip is |
Nick Coghlan | aa029da | 2014-02-09 10:10:24 +1000 | [diff] [blame] | 114 | installed in the virtual environment. This uses :mod:`ensurepip` with |
| 115 | the ``--default-pip`` option. |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 116 | |
Vinay Sajip | fd0f84b | 2016-08-06 10:43:44 +0100 | [diff] [blame] | 117 | * ``prompt`` -- a String to be used after virtual environment is activated |
| 118 | (defaults to ``None`` which means directory name of the environment would |
| 119 | be used). |
| 120 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 121 | .. versionchanged:: 3.4 |
| 122 | Added the ``with_pip`` parameter |
| 123 | |
Vinay Sajip | fd0f84b | 2016-08-06 10:43:44 +0100 | [diff] [blame] | 124 | .. versionadded:: 3.6 |
| 125 | Added the ``prompt`` parameter |
| 126 | |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 127 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 128 | Creators of third-party virtual environment tools will be free to use the |
| 129 | provided ``EnvBuilder`` class as a base class. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 130 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 131 | The returned env-builder is an object which has a method, ``create``: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 132 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 133 | .. method:: create(env_dir) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 134 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 135 | This method takes as required argument the path (absolute or relative to |
| 136 | the current directory) of the target directory which is to contain the |
| 137 | virtual environment. The ``create`` method will either create the |
| 138 | environment in the specified directory, or raise an appropriate |
| 139 | exception. |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 140 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 141 | The ``create`` method of the ``EnvBuilder`` class illustrates the hooks |
| 142 | available for subclass customization:: |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 143 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 144 | def create(self, env_dir): |
| 145 | """ |
| 146 | Create a virtualized Python environment in a directory. |
| 147 | env_dir is the target directory to create an environment in. |
| 148 | """ |
| 149 | env_dir = os.path.abspath(env_dir) |
Georg Brandl | a0b7923 | 2013-10-06 12:52:49 +0200 | [diff] [blame] | 150 | context = self.ensure_directories(env_dir) |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 151 | self.create_configuration(context) |
| 152 | self.setup_python(context) |
| 153 | self.setup_scripts(context) |
| 154 | self.post_setup(context) |
Vinay Sajip | 7ded1f0 | 2012-05-26 03:45:29 +0100 | [diff] [blame] | 155 | |
Georg Brandl | a0b7923 | 2013-10-06 12:52:49 +0200 | [diff] [blame] | 156 | Each of the methods :meth:`ensure_directories`, |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 157 | :meth:`create_configuration`, :meth:`setup_python`, |
| 158 | :meth:`setup_scripts` and :meth:`post_setup` can be overridden. |
| 159 | |
Vinay Sajip | 577d4ff | 2013-07-12 21:52:51 +0100 | [diff] [blame] | 160 | .. method:: ensure_directories(env_dir) |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 161 | |
| 162 | Creates the environment directory and all necessary directories, and |
| 163 | returns a context object. This is just a holder for attributes (such as |
Vinay Sajip | 577d4ff | 2013-07-12 21:52:51 +0100 | [diff] [blame] | 164 | paths), for use by the other methods. The directories are allowed to |
| 165 | exist already, as long as either ``clear`` or ``upgrade`` were |
| 166 | specified to allow operating on an existing environment directory. |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 167 | |
| 168 | .. method:: create_configuration(context) |
| 169 | |
| 170 | Creates the ``pyvenv.cfg`` configuration file in the environment. |
| 171 | |
| 172 | .. method:: setup_python(context) |
| 173 | |
| 174 | Creates a copy of the Python executable (and, under Windows, DLLs) in |
Vinay Sajip | 577d4ff | 2013-07-12 21:52:51 +0100 | [diff] [blame] | 175 | the environment. On a POSIX system, if a specific executable |
| 176 | ``python3.x`` was used, symlinks to ``python`` and ``python3`` will be |
| 177 | created pointing to that executable, unless files with those names |
| 178 | already exist. |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 179 | |
| 180 | .. method:: setup_scripts(context) |
| 181 | |
| 182 | Installs activation scripts appropriate to the platform into the virtual |
| 183 | environment. |
| 184 | |
| 185 | .. method:: post_setup(context) |
| 186 | |
| 187 | A placeholder method which can be overridden in third party |
| 188 | implementations to pre-install packages in the virtual environment or |
| 189 | perform other post-creation steps. |
| 190 | |
| 191 | In addition, :class:`EnvBuilder` provides this utility method that can be |
| 192 | called from :meth:`setup_scripts` or :meth:`post_setup` in subclasses to |
| 193 | assist in installing custom scripts into the virtual environment. |
| 194 | |
| 195 | .. method:: install_scripts(context, path) |
| 196 | |
| 197 | *path* is the path to a directory that should contain subdirectories |
| 198 | "common", "posix", "nt", each containing scripts destined for the bin |
| 199 | directory in the environment. The contents of "common" and the |
| 200 | directory corresponding to :data:`os.name` are copied after some text |
| 201 | replacement of placeholders: |
| 202 | |
| 203 | * ``__VENV_DIR__`` is replaced with the absolute path of the environment |
| 204 | directory. |
| 205 | |
| 206 | * ``__VENV_NAME__`` is replaced with the environment name (final path |
| 207 | segment of environment directory). |
| 208 | |
Vinay Sajip | dff9e25 | 2013-10-02 11:36:16 +0100 | [diff] [blame] | 209 | * ``__VENV_PROMPT__`` is replaced with the prompt (the environment |
| 210 | name surrounded by parentheses and with a following space) |
| 211 | |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 212 | * ``__VENV_BIN_NAME__`` is replaced with the name of the bin directory |
| 213 | (either ``bin`` or ``Scripts``). |
| 214 | |
| 215 | * ``__VENV_PYTHON__`` is replaced with the absolute path of the |
| 216 | environment's executable. |
| 217 | |
Vinay Sajip | 577d4ff | 2013-07-12 21:52:51 +0100 | [diff] [blame] | 218 | The directories are allowed to exist (for when an existing environment |
| 219 | is being upgraded). |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 220 | |
| 221 | There is also a module-level convenience function: |
| 222 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 223 | .. function:: create(env_dir, system_site_packages=False, clear=False, \ |
| 224 | symlinks=False, with_pip=False) |
Georg Brandl | dbab58f | 2012-06-24 16:37:59 +0200 | [diff] [blame] | 225 | |
| 226 | Create an :class:`EnvBuilder` with the given keyword arguments, and call its |
| 227 | :meth:`~EnvBuilder.create` method with the *env_dir* argument. |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 228 | |
Nick Coghlan | 8fbdb09 | 2013-11-23 00:30:34 +1000 | [diff] [blame] | 229 | .. versionchanged:: 3.4 |
| 230 | Added the ``with_pip`` parameter |
| 231 | |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 232 | An example of extending ``EnvBuilder`` |
| 233 | -------------------------------------- |
| 234 | |
| 235 | The following script shows how to extend :class:`EnvBuilder` by implementing a |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 236 | subclass which installs setuptools and pip into a created virtual environment:: |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 237 | |
| 238 | import os |
| 239 | import os.path |
| 240 | from subprocess import Popen, PIPE |
| 241 | import sys |
| 242 | from threading import Thread |
| 243 | from urllib.parse import urlparse |
| 244 | from urllib.request import urlretrieve |
| 245 | import venv |
| 246 | |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 247 | class ExtendedEnvBuilder(venv.EnvBuilder): |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 248 | """ |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 249 | This builder installs setuptools and pip so that you can pip or |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 250 | easy_install other packages into the created virtual environment. |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 251 | |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 252 | :param nodist: If True, setuptools and pip are not installed into the |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 253 | created virtual environment. |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 254 | :param nopip: If True, pip is not installed into the created |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 255 | virtual environment. |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 256 | :param progress: If setuptools or pip are installed, the progress of the |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 257 | installation can be monitored by passing a progress |
| 258 | callable. If specified, it is called with two |
| 259 | arguments: a string indicating some progress, and a |
| 260 | context indicating where the string is coming from. |
| 261 | The context argument can have one of three values: |
| 262 | 'main', indicating that it is called from virtualize() |
| 263 | itself, and 'stdout' and 'stderr', which are obtained |
| 264 | by reading lines from the output streams of a subprocess |
| 265 | which is used to install the app. |
| 266 | |
| 267 | If a callable is not specified, default progress |
| 268 | information is output to sys.stderr. |
| 269 | """ |
| 270 | |
| 271 | def __init__(self, *args, **kwargs): |
| 272 | self.nodist = kwargs.pop('nodist', False) |
| 273 | self.nopip = kwargs.pop('nopip', False) |
| 274 | self.progress = kwargs.pop('progress', None) |
| 275 | self.verbose = kwargs.pop('verbose', False) |
| 276 | super().__init__(*args, **kwargs) |
| 277 | |
| 278 | def post_setup(self, context): |
| 279 | """ |
| 280 | Set up any packages which need to be pre-installed into the |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 281 | virtual environment being created. |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 282 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 283 | :param context: The information for the virtual environment |
| 284 | creation request being processed. |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 285 | """ |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 286 | os.environ['VIRTUAL_ENV'] = context.env_dir |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 287 | if not self.nodist: |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 288 | self.install_setuptools(context) |
| 289 | # Can't install pip without setuptools |
| 290 | if not self.nopip and not self.nodist: |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 291 | self.install_pip(context) |
| 292 | |
| 293 | def reader(self, stream, context): |
| 294 | """ |
| 295 | Read lines from a subprocess' output stream and either pass to a progress |
| 296 | callable (if specified) or write progress information to sys.stderr. |
| 297 | """ |
| 298 | progress = self.progress |
| 299 | while True: |
| 300 | s = stream.readline() |
| 301 | if not s: |
| 302 | break |
| 303 | if progress is not None: |
| 304 | progress(s, context) |
| 305 | else: |
| 306 | if not self.verbose: |
| 307 | sys.stderr.write('.') |
| 308 | else: |
| 309 | sys.stderr.write(s.decode('utf-8')) |
| 310 | sys.stderr.flush() |
Vinay Sajip | ad6bb03 | 2013-07-12 21:44:35 +0100 | [diff] [blame] | 311 | stream.close() |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 312 | |
| 313 | def install_script(self, context, name, url): |
| 314 | _, _, path, _, _, _ = urlparse(url) |
| 315 | fn = os.path.split(path)[-1] |
| 316 | binpath = context.bin_path |
| 317 | distpath = os.path.join(binpath, fn) |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 318 | # Download script into the virtual environment's binaries folder |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 319 | urlretrieve(url, distpath) |
| 320 | progress = self.progress |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 321 | if self.verbose: |
| 322 | term = '\n' |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 323 | else: |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 324 | term = '' |
| 325 | if progress is not None: |
| 326 | progress('Installing %s ...%s' % (name, term), 'main') |
| 327 | else: |
| 328 | sys.stderr.write('Installing %s ...%s' % (name, term)) |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 329 | sys.stderr.flush() |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 330 | # Install in the virtual environment |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 331 | args = [context.env_exe, fn] |
| 332 | p = Popen(args, stdout=PIPE, stderr=PIPE, cwd=binpath) |
| 333 | t1 = Thread(target=self.reader, args=(p.stdout, 'stdout')) |
| 334 | t1.start() |
| 335 | t2 = Thread(target=self.reader, args=(p.stderr, 'stderr')) |
| 336 | t2.start() |
| 337 | p.wait() |
| 338 | t1.join() |
| 339 | t2.join() |
| 340 | if progress is not None: |
| 341 | progress('done.', 'main') |
| 342 | else: |
| 343 | sys.stderr.write('done.\n') |
| 344 | # Clean up - no longer needed |
| 345 | os.unlink(distpath) |
| 346 | |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 347 | def install_setuptools(self, context): |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 348 | """ |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 349 | Install setuptools in the virtual environment. |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 350 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 351 | :param context: The information for the virtual environment |
| 352 | creation request being processed. |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 353 | """ |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 354 | url = 'https://bitbucket.org/pypa/setuptools/downloads/ez_setup.py' |
| 355 | self.install_script(context, 'setuptools', url) |
| 356 | # clear up the setuptools archive which gets downloaded |
| 357 | pred = lambda o: o.startswith('setuptools-') and o.endswith('.tar.gz') |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 358 | files = filter(pred, os.listdir(context.bin_path)) |
| 359 | for f in files: |
| 360 | f = os.path.join(context.bin_path, f) |
| 361 | os.unlink(f) |
| 362 | |
| 363 | def install_pip(self, context): |
| 364 | """ |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 365 | Install pip in the virtual environment. |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 366 | |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 367 | :param context: The information for the virtual environment |
| 368 | creation request being processed. |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 369 | """ |
| 370 | url = 'https://raw.github.com/pypa/pip/master/contrib/get-pip.py' |
| 371 | self.install_script(context, 'pip', url) |
| 372 | |
| 373 | def main(args=None): |
| 374 | compatible = True |
| 375 | if sys.version_info < (3, 3): |
| 376 | compatible = False |
| 377 | elif not hasattr(sys, 'base_prefix'): |
| 378 | compatible = False |
| 379 | if not compatible: |
| 380 | raise ValueError('This script is only for use with ' |
| 381 | 'Python 3.3 or later') |
| 382 | else: |
| 383 | import argparse |
| 384 | |
| 385 | parser = argparse.ArgumentParser(prog=__name__, |
| 386 | description='Creates virtual Python ' |
| 387 | 'environments in one or ' |
| 388 | 'more target ' |
| 389 | 'directories.') |
| 390 | parser.add_argument('dirs', metavar='ENV_DIR', nargs='+', |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 391 | help='A directory in which to create the |
| 392 | 'virtual environment.') |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 393 | parser.add_argument('--no-setuptools', default=False, |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 394 | action='store_true', dest='nodist', |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 395 | help="Don't install setuptools or pip in the " |
| 396 | "virtual environment.") |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 397 | parser.add_argument('--no-pip', default=False, |
| 398 | action='store_true', dest='nopip', |
| 399 | help="Don't install pip in the virtual " |
| 400 | "environment.") |
| 401 | parser.add_argument('--system-site-packages', default=False, |
| 402 | action='store_true', dest='system_site', |
| 403 | help='Give the virtual environment access to the ' |
| 404 | 'system site-packages dir.') |
| 405 | if os.name == 'nt': |
| 406 | use_symlinks = False |
| 407 | else: |
| 408 | use_symlinks = True |
| 409 | parser.add_argument('--symlinks', default=use_symlinks, |
| 410 | action='store_true', dest='symlinks', |
| 411 | help='Try to use symlinks rather than copies, ' |
| 412 | 'when symlinks are not the default for ' |
| 413 | 'the platform.') |
| 414 | parser.add_argument('--clear', default=False, action='store_true', |
| 415 | dest='clear', help='Delete the contents of the ' |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 416 | 'virtual environment ' |
| 417 | 'directory if it already ' |
| 418 | 'exists, before virtual ' |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 419 | 'environment creation.') |
| 420 | parser.add_argument('--upgrade', default=False, action='store_true', |
Brett Cannon | 15552c3 | 2016-07-08 10:46:21 -0700 | [diff] [blame] | 421 | dest='upgrade', help='Upgrade the virtual ' |
| 422 | 'environment directory to ' |
| 423 | 'use this version of ' |
| 424 | 'Python, assuming Python ' |
| 425 | 'has been upgraded ' |
| 426 | 'in-place.') |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 427 | parser.add_argument('--verbose', default=False, action='store_true', |
| 428 | dest='verbose', help='Display the output ' |
| 429 | 'from the scripts which ' |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 430 | 'install setuptools and pip.') |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 431 | options = parser.parse_args(args) |
| 432 | if options.upgrade and options.clear: |
| 433 | raise ValueError('you cannot supply --upgrade and --clear together.') |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 434 | builder = ExtendedEnvBuilder(system_site_packages=options.system_site, |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 435 | clear=options.clear, |
| 436 | symlinks=options.symlinks, |
| 437 | upgrade=options.upgrade, |
| 438 | nodist=options.nodist, |
| 439 | nopip=options.nopip, |
| 440 | verbose=options.verbose) |
| 441 | for d in options.dirs: |
| 442 | builder.create(d) |
| 443 | |
| 444 | if __name__ == '__main__': |
| 445 | rc = 1 |
| 446 | try: |
| 447 | main() |
| 448 | rc = 0 |
| 449 | except Exception as e: |
| 450 | print('Error: %s' % e, file=sys.stderr) |
| 451 | sys.exit(rc) |
| 452 | |
Vinay Sajip | 3c557f2 | 2013-07-12 20:54:25 +0100 | [diff] [blame] | 453 | |
Vinay Sajip | 2b4fcfb | 2013-01-30 13:44:00 +0000 | [diff] [blame] | 454 | This script is also available for download `online |
| 455 | <https://gist.github.com/4673395>`_. |