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