@@ -37,14 +37,14 @@ A virtual environment is (amongst other things):
37
37
are by default isolated from software in other virtual environments and Python
38
38
interpreters and libraries installed in the operating system.
39
39
40
- * Contained in a directory, conventionally either named ``venv `` or ``. venv `` in
40
+ * Contained in a directory, conventionally named ``. venv `` or ``venv `` in
41
41
the project directory, or under a container directory for lots of virtual
42
42
environments, such as ``~/.virtualenvs ``.
43
43
44
44
* Not checked into source control systems such as Git.
45
45
46
46
* Considered as disposable -- it should be simple to delete and recreate it from
47
- scratch. You don't place any project code in the environment
47
+ scratch. You don't place any project code in the environment.
48
48
49
49
* Not considered as movable or copyable -- you just recreate the same
50
50
environment in the target location.
@@ -61,7 +61,117 @@ See :pep:`405` for more background on Python virtual environments.
61
61
Creating virtual environments
62
62
-----------------------------
63
63
64
- .. include :: /using/venv-create.inc
64
+ :ref: `Virtual environments <venv-def >` are created by executing the ``venv ``
65
+ module:
66
+
67
+ .. code-block :: shell
68
+
69
+ python -m venv /path/to/new/virtual/environment
70
+
71
+ This creates the target directory (including parent directories as needed)
72
+ and places a :file: `pyvenv.cfg ` file in it with a ``home `` key
73
+ pointing to the Python installation from which the command was run.
74
+ It also creates a :file: `bin ` (or :file: `Scripts ` on Windows) subdirectory
75
+ containing a copy or symlink of the Python executable
76
+ (as appropriate for the platform or arguments used at environment creation time).
77
+ It also creates a :file: `lib/pythonX.Y/site-packages ` subdirectory
78
+ (on Windows, this is :file: `Lib\s ite-packages `).
79
+ If an existing directory is specified, it will be re-used.
80
+
81
+ .. versionchanged :: 3.5
82
+ The use of ``venv `` is now recommended for creating virtual environments.
83
+
84
+ .. deprecated-removed :: 3.6 3.8
85
+ :program: `pyvenv ` was the recommended tool for creating virtual environments
86
+ for Python 3.3 and 3.4, and replaced in 3.5 by executing ``venv `` directly.
87
+
88
+ .. highlight :: none
89
+
90
+ On Windows, invoke the ``venv `` command as follows:
91
+
92
+ .. code-block :: ps1con
93
+
94
+ PS> python -m venv C:\path\to\new\virtual\environment
95
+
96
+ The command, if run with ``-h ``, will show the available options::
97
+
98
+ usage: venv [-h] [--system-site-packages] [--symlinks | --copies] [--clear]
99
+ [--upgrade] [--without-pip] [--prompt PROMPT] [--upgrade-deps]
100
+ ENV_DIR [ENV_DIR ...]
101
+
102
+ Creates virtual Python environments in one or more target directories.
103
+
104
+ positional arguments:
105
+ ENV_DIR A directory to create the environment in.
106
+
107
+ options:
108
+ -h, --help show this help message and exit
109
+ --system-site-packages
110
+ Give the virtual environment access to the system
111
+ site-packages dir.
112
+ --symlinks Try to use symlinks rather than copies, when
113
+ symlinks are not the default for the platform.
114
+ --copies Try to use copies rather than symlinks, even when
115
+ symlinks are the default for the platform.
116
+ --clear Delete the contents of the environment directory
117
+ if it already exists, before environment creation.
118
+ --upgrade Upgrade the environment directory to use this
119
+ version of Python, assuming Python has been
120
+ upgraded in-place.
121
+ --without-pip Skips installing or upgrading pip in the virtual
122
+ environment (pip is bootstrapped by default)
123
+ --prompt PROMPT Provides an alternative prompt prefix for this
124
+ environment.
125
+ --upgrade-deps Upgrade core dependencies (pip) to the latest
126
+ version in PyPI
127
+
128
+ Once an environment has been created, you may wish to activate it, e.g. by
129
+ sourcing an activate script in its bin directory.
130
+
131
+
132
+ .. versionchanged :: 3.4
133
+ Installs pip by default, added the ``--without-pip `` and ``--copies ``
134
+ options.
135
+
136
+ .. versionchanged :: 3.4
137
+ In earlier versions, if the target directory already existed, an error was
138
+ raised, unless the ``--clear `` or ``--upgrade `` option was provided.
139
+
140
+ .. versionchanged :: 3.9
141
+ Add ``--upgrade-deps `` option to upgrade pip + setuptools to the latest on PyPI.
142
+
143
+ .. versionchanged :: 3.12
144
+
145
+ ``setuptools `` is no longer a core venv dependency.
146
+
147
+ .. note ::
148
+ While symlinks are supported on Windows, they are not recommended. Of
149
+ particular note is that double-clicking ``python.exe `` in File Explorer
150
+ will resolve the symlink eagerly and ignore the virtual environment.
151
+
152
+ .. note ::
153
+ On Microsoft Windows, it may be required to enable the ``Activate.ps1 ``
154
+ script by setting the execution policy for the user. You can do this by
155
+ issuing the following PowerShell command:
156
+
157
+ .. code-block :: powershell
158
+
159
+ PS C:\> Set-ExecutionPolicy - ExecutionPolicy RemoteSigned - Scope CurrentUser
160
+
161
+ See `About Execution Policies
162
+ <https://go.microsoft.com/fwlink/?LinkID=135170> `_
163
+ for more information.
164
+
165
+ The created :file: `pyvenv.cfg ` file also includes the
166
+ ``include-system-site-packages `` key, set to ``true `` if ``venv `` is
167
+ run with the ``--system-site-packages `` option, ``false `` otherwise.
168
+
169
+ Unless the ``--without-pip `` option is given, :mod: `ensurepip ` will be
170
+ invoked to bootstrap ``pip `` into the virtual environment.
171
+
172
+ Multiple paths can be given to ``venv ``, in which case an identical virtual
173
+ environment will be created, according to the given options, at each provided
174
+ path.
65
175
66
176
.. _venv-explanation :
67
177
@@ -117,7 +227,7 @@ should be runnable without activating it.
117
227
118
228
In order to achieve this, scripts installed into virtual environments have
119
229
a "shebang" line which points to the environment's Python interpreter,
120
- i.e. :samp: `#!/{ <path-to-venv> } /bin/python `.
230
+ :samp: `#!/{ <path-to-venv> } /bin/python `.
121
231
This means that the script will run with that interpreter regardless of the
122
232
value of :envvar: `PATH `. On Windows, "shebang" line processing is supported if
123
233
you have the :ref: `launcher ` installed. Thus, double-clicking an installed
@@ -322,8 +432,8 @@ creation according to their needs, the :class:`EnvBuilder` class.
322
432
assist in installing custom scripts into the virtual environment.
323
433
324
434
*path * is the path to a directory that should contain subdirectories
325
- " common", " posix", "nt", each containing scripts destined for the bin
326
- directory in the environment. The contents of " common" and the
435
+ `` common ``, `` posix ``, `` nt ``; each containing scripts destined for the
436
+ `` bin `` directory in the environment. The contents of `` common `` and the
327
437
directory corresponding to :data: `os.name ` are copied after some text
328
438
replacement of placeholders:
329
439
@@ -367,13 +477,13 @@ There is also a module-level convenience function:
367
477
.. versionadded :: 3.3
368
478
369
479
.. versionchanged :: 3.4
370
- Added the `` with_pip `` parameter
480
+ Added the * with_pip * parameter
371
481
372
482
.. versionchanged :: 3.6
373
- Added the `` prompt `` parameter
483
+ Added the * prompt * parameter
374
484
375
485
.. versionchanged :: 3.9
376
- Added the `` upgrade_deps `` parameter
486
+ Added the * upgrade_deps * parameter
377
487
378
488
An example of extending ``EnvBuilder ``
379
489
--------------------------------------
0 commit comments