15
15
16
16
--------------
17
17
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 (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.
18
+ .. _venv-def :
19
+ .. _venv-intro :
20
+
21
+ The :mod: `!venv ` module supports creating lightweight "virtual environments",
22
+ each with their own independent set of Python packages installed in
23
+ their :mod: `site ` directories.
24
+ A virtual environment is created on top of an existing
25
+ Python installation, known as the virtual environment's "base" Python, and may
26
+ optionally be isolated from the packages in the base environment,
27
+ so only those explicitly installed in the virtual environment are available.
28
+
29
+ When used from within a virtual environment, common installation tools such as
30
+ `pip `_ will install Python packages into a virtual environment
31
+ without needing to be told to do so explicitly.
24
32
25
- See :pep: `405 ` for more information about Python virtual environments.
33
+ See :pep: `405 ` for more background on Python virtual environments.
26
34
27
35
.. seealso ::
28
36
29
37
`Python Packaging User Guide: Creating and using virtual environments
30
38
<https://packaging.python.org/guides/installing-using-pip-and-virtual-environments/#creating-a-virtual-environment> `__
31
39
32
-
33
40
Creating virtual environments
34
41
-----------------------------
35
42
36
43
.. include :: /using/venv-create.inc
37
44
45
+ .. _venv-explanation :
38
46
39
- .. _venv-def :
47
+ How venvs work
48
+ --------------
40
49
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.
46
-
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.
49
-
50
- Common installation tools such as setuptools _ and pip _ work as
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.
54
-
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).
65
-
66
- When a virtual environment is active, any options that change the
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.
70
-
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
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 ``.
50
+ When a Python interpreter is running from a virtual environment,
51
+ :data: `sys.prefix ` and :data: `sys.exec_prefix `
52
+ point to the directories of the virtual environment,
53
+ whereas :data: `sys.base_prefix ` and :data: `sys.base_exec_prefix `
54
+ point to those of the base Python used to create the environment.
55
+ It is sufficient to check
56
+ ``sys.prefix == sys.base_prefix `` to determine if the current interpreter is
57
+ running from a virtual environment.
58
+
59
+ A virtual environment may be "activated" using a script in its binary directory
60
+ (``bin `` on POSIX; ``Scripts `` on Windows).
61
+ This will prepend that directory to your :envvar: `!PATH `, so that running
62
+ :program: `!python ` will invoke the environment's Python interpreter
63
+ and you can run installed scripts without having to use their full path.
64
+ The invocation of the activation script is platform-specific
65
+ (:samp: `{ <venv> } ` must be replaced by the path to the directory
66
+ containing the virtual environment):
67
+
68
+ +-------------+------------+--------------------------------------------------+
69
+ | Platform | Shell | Command to activate virtual environment |
70
+ +=============+============+==================================================+
71
+ | POSIX | bash/zsh | :samp: `$ source { <venv> } /bin/activate ` |
72
+ | +------------+--------------------------------------------------+
73
+ | | fish | :samp: `$ source { <venv> } /bin/activate.fish ` |
74
+ | +------------+--------------------------------------------------+
75
+ | | csh/tcsh | :samp: `$ source { <venv> } /bin/activate.csh ` |
76
+ | +------------+--------------------------------------------------+
77
+ | | PowerShell | :samp: `$ { <venv> } /bin/Activate.ps1 ` |
78
+ +-------------+------------+--------------------------------------------------+
79
+ | Windows | cmd.exe | :samp: `C:\\ > { <venv> } \\ Scripts\\ activate.bat ` |
80
+ | +------------+--------------------------------------------------+
81
+ | | PowerShell | :samp: `PS C:\\ > { <venv> } \\ Scripts\\ Activate.ps1 ` |
82
+ +-------------+------------+--------------------------------------------------+
83
+
84
+ .. versionadded :: 3.4
85
+ :program: `!fish ` and :program: `!csh ` activation scripts.
86
+
87
+ .. versionadded :: 3.8
88
+ PowerShell activation scripts installed under POSIX for PowerShell Core
89
+ support.
90
+
91
+ You don't specifically *need * to activate a virtual environment,
92
+ as you can just specify the full path to that environment's
93
+ Python interpreter when invoking Python.
94
+ Furthermore, all scripts installed in the environment
95
+ should be runnable without activating it.
96
+
97
+ In order to achieve this, scripts installed into virtual environments have
98
+ a "shebang" line which points to the environment's Python interpreter,
99
+ i.e. :samp: `#!/{ <path-to-venv> } /bin/python `.
100
+ This means that the script will run with that interpreter regardless of the
101
+ value of :envvar: `!PATH `. On Windows, "shebang" line processing is supported if
102
+ you have the :ref: `launcher ` installed. Thus, double-clicking an installed
103
+ script in a Windows Explorer window should run it with the correct interpreter
104
+ without the environment needing to be activated or on the :envvar: `!PATH `.
105
+
106
+ When a virtual environment has been activated, the :envvar: `!VIRTUAL_ENV `
107
+ environment variable is set to the path of the environment.
108
+ Since explicitly activating a virtual environment is not required to use it,
109
+ :envvar: `!VIRTUAL_ENV ` cannot be relied upon to determine
110
+ whether a virtual environment is being used.
86
111
87
112
.. warning :: Because scripts installed in environments should not expect the
88
113
environment to be activated, their shebang lines contain the absolute paths
@@ -98,6 +123,11 @@ Creating virtual environments
98
123
environment in its new location. Otherwise, software installed into the
99
124
environment may not work as expected.
100
125
126
+ You can deactivate a virtual environment by typing ``deactivate `` in your shell.
127
+ The exact mechanism is platform-specific and is an internal implementation
128
+ detail (typically, a script or shell function will be used).
129
+
130
+
101
131
.. _venv-api :
102
132
103
133
API
@@ -183,11 +213,56 @@ creation according to their needs, the :class:`EnvBuilder` class.
183
213
184
214
.. method :: ensure_directories(env_dir)
185
215
186
- Creates the environment directory and all necessary directories, and
187
- returns a context object. This is just a holder for attributes (such as
188
- paths), for use by the other methods. The directories are allowed to
189
- exist already, as long as either ``clear `` or ``upgrade `` were
190
- specified to allow operating on an existing environment directory.
216
+ Creates the environment directory and all necessary subdirectories that
217
+ don't already exist, and returns a context object. This context object
218
+ is just a holder for attributes (such as paths) for use by the other
219
+ methods. If the :class: `EnvBuilder ` is created with the arg
220
+ ``clear=True ``, contents of the environment directory will be cleared
221
+ and then all necessary subdirectories will be recreated.
222
+
223
+ The returned context object is a :class: `types.SimpleNamespace ` with the
224
+ following attributes:
225
+
226
+ * ``env_dir `` - The location of the virtual environment. Used for
227
+ ``__VENV_DIR__ `` in activation scripts (see :meth: `install_scripts `).
228
+
229
+ * ``env_name `` - The name of the virtual environment. Used for
230
+ ``__VENV_NAME__ `` in activation scripts (see :meth: `install_scripts `).
231
+
232
+ * ``prompt `` - The prompt to be used by the activation scripts. Used for
233
+ ``__VENV_PROMPT__ `` in activation scripts (see :meth: `install_scripts `).
234
+
235
+ * ``executable `` - The underlying Python executable used by the virtual
236
+ environment. This takes into account the case where a virtual environment
237
+ is created from another virtual environment.
238
+
239
+ * ``inc_path `` - The include path for the virtual environment.
240
+
241
+ * ``lib_path `` - The purelib path for the virtual environment.
242
+
243
+ * ``bin_path `` - The script path for the virtual environment.
244
+
245
+ * ``bin_name `` - The name of the script path relative to the virtual
246
+ environment location. Used for ``__VENV_BIN_NAME__ `` in activation
247
+ scripts (see :meth: `install_scripts `).
248
+
249
+ * ``env_exe `` - The name of the Python interpreter in the virtual
250
+ environment. Used for ``__VENV_PYTHON__ `` in activation scripts
251
+ (see :meth: `install_scripts `).
252
+
253
+ * ``env_exec_cmd `` - The name of the Python interpreter, taking into
254
+ account filesystem redirections. This can be used to run Python in
255
+ the virtual environment.
256
+
257
+
258
+ .. versionchanged :: 3.12
259
+ The attribute ``lib_path `` was added to the context, and the context
260
+ object was documented.
261
+
262
+ .. versionchanged :: 3.11
263
+ The *venv *
264
+ :ref: `sysconfig installation scheme <installation_paths >`
265
+ is used to construct the paths of the created directories.
191
266
192
267
.. method :: create_configuration(context)
193
268
0 commit comments