Skip to content

Commit 0037717

Browse files
hugovkCAM-GerlachAA-Turner
authored
[3.12] Docs: Update and proofread library/venv.rst (GH-124121) (#124680)
* [3.12] Docs: Update and proofread `library/venv.rst` (GH-124121) (cherry picked from commit 23e812b) Co-authored-by: Hugo van Kemenade <[email protected]> Co-authored-by: C.A.M. Gerlach <[email protected]> Co-authored-by: Adam Turner <[email protected]> Co-authored-by: Hugo van Kemenade <[email protected]> * Remove 3.13 docs --------- Co-authored-by: C.A.M. Gerlach <[email protected]> Co-authored-by: Adam Turner <[email protected]>
1 parent 7c39f74 commit 0037717

File tree

1 file changed

+119
-9
lines changed

1 file changed

+119
-9
lines changed

Doc/library/venv.rst

Lines changed: 119 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -37,14 +37,14 @@ A virtual environment is (amongst other things):
3737
are by default isolated from software in other virtual environments and Python
3838
interpreters and libraries installed in the operating system.
3939

40-
* Contained in a directory, conventionally either named ``venv`` or ``.venv`` in
40+
* Contained in a directory, conventionally named ``.venv`` or ``venv`` in
4141
the project directory, or under a container directory for lots of virtual
4242
environments, such as ``~/.virtualenvs``.
4343

4444
* Not checked into source control systems such as Git.
4545

4646
* 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.
4848

4949
* Not considered as movable or copyable -- you just recreate the same
5050
environment in the target location.
@@ -61,7 +61,117 @@ See :pep:`405` for more background on Python virtual environments.
6161
Creating virtual environments
6262
-----------------------------
6363

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\site-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.
65175

66176
.. _venv-explanation:
67177

@@ -117,7 +227,7 @@ should be runnable without activating it.
117227

118228
In order to achieve this, scripts installed into virtual environments have
119229
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`.
121231
This means that the script will run with that interpreter regardless of the
122232
value of :envvar:`PATH`. On Windows, "shebang" line processing is supported if
123233
you have the :ref:`launcher` installed. Thus, double-clicking an installed
@@ -322,8 +432,8 @@ creation according to their needs, the :class:`EnvBuilder` class.
322432
assist in installing custom scripts into the virtual environment.
323433

324434
*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
327437
directory corresponding to :data:`os.name` are copied after some text
328438
replacement of placeholders:
329439

@@ -367,13 +477,13 @@ There is also a module-level convenience function:
367477
.. versionadded:: 3.3
368478

369479
.. versionchanged:: 3.4
370-
Added the ``with_pip`` parameter
480+
Added the *with_pip* parameter
371481

372482
.. versionchanged:: 3.6
373-
Added the ``prompt`` parameter
483+
Added the *prompt* parameter
374484

375485
.. versionchanged:: 3.9
376-
Added the ``upgrade_deps`` parameter
486+
Added the *upgrade_deps* parameter
377487

378488
An example of extending ``EnvBuilder``
379489
--------------------------------------

0 commit comments

Comments
 (0)