@@ -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,127 @@ 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
+ [--without-scm-ignore-files]
101
+ ENV_DIR [ENV_DIR ...]
102
+
103
+ Creates virtual Python environments in one or more target directories.
104
+
105
+ positional arguments:
106
+ ENV_DIR A directory to create the environment in.
107
+
108
+ options:
109
+ -h, --help show this help message and exit
110
+ --system-site-packages
111
+ Give the virtual environment access to the system
112
+ site-packages dir.
113
+ --symlinks Try to use symlinks rather than copies, when
114
+ symlinks are not the default for the platform.
115
+ --copies Try to use copies rather than symlinks, even when
116
+ symlinks are the default for the platform.
117
+ --clear Delete the contents of the environment directory
118
+ if it already exists, before environment creation.
119
+ --upgrade Upgrade the environment directory to use this
120
+ version of Python, assuming Python has been
121
+ upgraded in-place.
122
+ --without-pip Skips installing or upgrading pip in the virtual
123
+ environment (pip is bootstrapped by default)
124
+ --prompt PROMPT Provides an alternative prompt prefix for this
125
+ environment.
126
+ --upgrade-deps Upgrade core dependencies (pip) to the latest
127
+ version in PyPI
128
+ --without-scm-ignore-files
129
+ Skips adding SCM ignore files to the environment
130
+ directory (Git is supported by default).
131
+
132
+ Once an environment has been created, you may wish to activate it, e.g. by
133
+ sourcing an activate script in its bin directory.
134
+
135
+
136
+ .. versionchanged :: 3.4
137
+ Installs pip by default, added the ``--without-pip `` and ``--copies ``
138
+ options.
139
+
140
+ .. versionchanged :: 3.4
141
+ In earlier versions, if the target directory already existed, an error was
142
+ raised, unless the ``--clear `` or ``--upgrade `` option was provided.
143
+
144
+ .. versionchanged :: 3.9
145
+ Add ``--upgrade-deps `` option to upgrade pip + setuptools to the latest on PyPI.
146
+
147
+ .. versionchanged :: 3.12
148
+
149
+ ``setuptools `` is no longer a core venv dependency.
150
+
151
+ .. versionchanged :: 3.13
152
+
153
+ Added the ``--without-scm-ignore-files `` option.
154
+ .. versionchanged :: 3.13
155
+ ``venv `` now creates a :file: `.gitignore ` file for Git by default.
156
+
157
+ .. note ::
158
+ While symlinks are supported on Windows, they are not recommended. Of
159
+ particular note is that double-clicking ``python.exe `` in File Explorer
160
+ will resolve the symlink eagerly and ignore the virtual environment.
161
+
162
+ .. note ::
163
+ On Microsoft Windows, it may be required to enable the ``Activate.ps1 ``
164
+ script by setting the execution policy for the user. You can do this by
165
+ issuing the following PowerShell command:
166
+
167
+ .. code-block :: powershell
168
+
169
+ PS C:\> Set-ExecutionPolicy - ExecutionPolicy RemoteSigned - Scope CurrentUser
170
+
171
+ See `About Execution Policies
172
+ <https://go.microsoft.com/fwlink/?LinkID=135170> `_
173
+ for more information.
174
+
175
+ The created :file: `pyvenv.cfg ` file also includes the
176
+ ``include-system-site-packages `` key, set to ``true `` if ``venv `` is
177
+ run with the ``--system-site-packages `` option, ``false `` otherwise.
178
+
179
+ Unless the ``--without-pip `` option is given, :mod: `ensurepip ` will be
180
+ invoked to bootstrap ``pip `` into the virtual environment.
181
+
182
+ Multiple paths can be given to ``venv ``, in which case an identical virtual
183
+ environment will be created, according to the given options, at each provided
184
+ path.
65
185
66
186
.. _venv-explanation :
67
187
@@ -117,7 +237,7 @@ should be runnable without activating it.
117
237
118
238
In order to achieve this, scripts installed into virtual environments have
119
239
a "shebang" line which points to the environment's Python interpreter,
120
- i.e. :samp: `#!/{ <path-to-venv> } /bin/python `.
240
+ :samp: `#!/{ <path-to-venv> } /bin/python `.
121
241
This means that the script will run with that interpreter regardless of the
122
242
value of :envvar: `PATH `. On Windows, "shebang" line processing is supported if
123
243
you have the :ref: `launcher ` installed. Thus, double-clicking an installed
@@ -345,8 +465,8 @@ creation according to their needs, the :class:`EnvBuilder` class.
345
465
.. method :: install_scripts(context, path)
346
466
347
467
*path * is the path to a directory that should contain subdirectories
348
- " common", " posix", "nt", each containing scripts destined for the bin
349
- directory in the environment. The contents of " common" and the
468
+ `` common ``, `` posix ``, `` nt ``; each containing scripts destined for the
469
+ `` bin `` directory in the environment. The contents of `` common `` and the
350
470
directory corresponding to :data: `os.name ` are copied after some text
351
471
replacement of placeholders:
352
472
@@ -371,7 +491,7 @@ creation according to their needs, the :class:`EnvBuilder` class.
371
491
.. method :: create_git_ignore_file(context)
372
492
373
493
Creates a ``.gitignore `` file within the virtual environment that causes
374
- the entire directory to be ignored by the `` git `` source control manager.
494
+ the entire directory to be ignored by the Git source control manager.
375
495
376
496
.. versionadded :: 3.13
377
497
@@ -387,16 +507,16 @@ There is also a module-level convenience function:
387
507
.. versionadded :: 3.3
388
508
389
509
.. versionchanged :: 3.4
390
- Added the `` with_pip `` parameter
510
+ Added the * with_pip * parameter
391
511
392
512
.. versionchanged :: 3.6
393
- Added the `` prompt `` parameter
513
+ Added the * prompt * parameter
394
514
395
515
.. versionchanged :: 3.9
396
- Added the `` upgrade_deps `` parameter
516
+ Added the * upgrade_deps * parameter
397
517
398
518
.. versionchanged :: 3.13
399
- Added the `` scm_ignore_files `` parameter
519
+ Added the * scm_ignore_files * parameter
400
520
401
521
An example of extending ``EnvBuilder ``
402
522
--------------------------------------
0 commit comments