Skip to content

Commit f8d3a40

Browse files
committed
Update how to guide for installing using pip and venv
1 parent d3e161e commit f8d3a40

File tree

1 file changed

+93
-95
lines changed

1 file changed

+93
-95
lines changed

source/guides/installing-using-pip-and-virtual-environments.rst

Lines changed: 93 additions & 95 deletions
Original file line numberDiff line numberDiff line change
@@ -1,22 +1,32 @@
1-
Installing packages using pip and virtual environments
2-
======================================================
1+
Installing packages using pip and venv
2+
======================================
33

44
This guide discusses how to install packages using :ref:`pip` and
5-
a virtual environment manager: either :ref:`venv` for Python 3 or :ref:`virtualenv`
6-
for Python 2. These are the lowest-level tools for managing Python
7-
packages and are recommended if higher-level tools do not suit your needs.
5+
the standard library's virtual environment manager :ref:`venv`. The guide
6+
covers how to:
87

9-
.. note:: This doc uses the term **package** to refer to a
10-
:term:`Distribution Package` which is different from an :term:`Import
11-
Package` that which is used to import modules in your Python source code.
8+
* Install and update pip
9+
* Create and use a virtual environment
10+
* Install packages into a virtual environment using the ``pip`` command
11+
* Use and create a requirements file
1212

1313

14-
Installing pip
15-
--------------
14+
.. note:: This guide applies to Python 3.3 and higher. If using a
15+
legacy version of Python 2.x, consider using :ref:`virtualenv`.
1616

17-
:ref:`pip` is the reference Python package manager. It's used to install and
18-
update packages. You'll need to make sure you have the latest version of pip
19-
installed.
17+
18+
.. note:: This guide uses the term **package** to refer to a
19+
:term:`Distribution Package`, which commonly is installed from an external
20+
host. This differs from the term :term:`Import Package` which refers to
21+
import modules in your Python source code.
22+
23+
24+
Install and update pip
25+
----------------------
26+
27+
:ref:`pip` is the reference Python package manager.
28+
It's used to install and update packages.
29+
Make sure you have the latest version of pip installed.
2030

2131

2232
.. tab:: Unix/macOS
@@ -59,94 +69,68 @@ installed.
5969
pip 21.1.3 from c:\python39\lib\site-packages (Python 3.9.4)
6070
6171
72+
Create and Use Virtual Environments
73+
-----------------------------------
6274

63-
Installing virtualenv
64-
---------------------
65-
66-
.. Note:: If you are using Python 3.3 or newer, the :mod:`venv` module is
67-
the preferred way to create and manage virtual environments.
68-
venv is included in the Python standard library and requires no additional installation.
69-
If you are using venv, you may skip this section.
70-
71-
72-
:ref:`virtualenv` is used to manage Python packages for different projects.
73-
Using virtualenv allows you to avoid installing Python packages globally
74-
which could break system tools or other projects. You can install virtualenv
75-
using pip.
75+
Create a new virtual environment
76+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
7677

78+
:ref:`venv` (for Python 3) allows you to manage separate package installations for
79+
different projects. It creates a "virtual" isolated Python installation. When
80+
you switch projects, you can create a new virtual environment which is isolated
81+
from other virtual environments. You benefit from the virtual environment
82+
since packages can be installed confidently and will not interfere with the
83+
other project environments.
7784

78-
.. tab:: Unix/macOS
79-
80-
.. code-block:: bash
81-
82-
python3 -m pip install --user virtualenv
83-
84-
.. tab:: Windows
85-
86-
.. code-block:: bat
87-
88-
py -m pip install --user virtualenv
89-
90-
91-
92-
Creating a virtual environment
93-
------------------------------
94-
95-
:ref:`venv` (for Python 3) and :ref:`virtualenv` (for Python 2) allow
96-
you to manage separate package installations for
97-
different projects. They essentially allow you to create a "virtual" isolated
98-
Python installation and install packages into that virtual installation. When
99-
you switch projects, you can simply create a new virtual environment and not
100-
have to worry about breaking the packages installed in the other environments.
101-
It is always recommended to use a virtual environment while developing Python
102-
applications.
85+
.. tip::
86+
It is always recommended to use a virtual environment while developing Python
87+
applications.
10388

10489
To create a virtual environment, go to your project's directory and run
105-
venv. If you are using Python 2, replace ``venv`` with ``virtualenv``
106-
in the below commands.
90+
``venv``.
10791

10892
.. tab:: Unix/macOS
10993

11094
.. code-block:: bash
11195
112-
python3 -m venv env
96+
python3 -m venv .venv
11397
11498
.. tab:: Windows
11599

116100
.. code-block:: bat
117101
118-
py -m venv env
102+
py -m venv .venv
119103
120104
The second argument is the location to create the virtual environment. Generally, you
121-
can just create this in your project and call it ``env``.
105+
can just create this in your project and call it ``.venv``.
122106

123-
venv will create a virtual Python installation in the ``env`` folder.
107+
``venv`` will create a virtual Python installation in the ``.venv`` folder.
124108

125109
.. Note:: You should exclude your virtual environment directory from your version
126110
control system using ``.gitignore`` or similar.
127111

128112

129-
Activating a virtual environment
130-
--------------------------------
113+
Activate a virtual environment
114+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131115

132116
Before you can start installing or using packages in your virtual environment you'll
133-
need to *activate* it. Activating a virtual environment will put the
134-
virtual environment-specific
135-
``python`` and ``pip`` executables into your shell's ``PATH``.
117+
need to ``activate`` it. Activating a virtual environment will put the
118+
virtual environment-specific ``python`` and ``pip`` executables into your
119+
shell's ``PATH``.
136120

137121
.. tab:: Unix/macOS
138122

139123
.. code-block:: bash
140124
141-
source env/bin/activate
125+
source .venv/bin/activate
142126
143127
.. tab:: Windows
144128

145129
.. code-block:: bat
146130
147-
.\env\Scripts\activate
131+
.\.venv\Scripts\activate
148132
149-
You can confirm you're in the virtual environment by checking the location of your
133+
To confirm the virtual environment is activated, check the location of your
150134
Python interpreter:
151135

152136
.. tab:: Unix/macOS
@@ -161,43 +145,56 @@ Python interpreter:
161145
162146
where python
163147
164-
It should be in the ``env`` directory:
148+
When the virtual environment is activated, the location will include
149+
the ``.venv`` directory:
165150

166151
.. tab:: Unix/macOS
167152

168153
.. code-block:: bash
169154
170-
.../env/bin/python
155+
.../.venv/bin/python
171156
172157
.. tab:: Windows
173158

174159
.. code-block:: bat
175160
176-
...\env\Scripts\python.exe
161+
...\.venv\Scripts\python.exe
177162
178163
179-
As long as your virtual environment is activated pip will install packages into that
180-
specific environment and you'll be able to import and use packages in your
164+
While a virtual environment is activated, pip will install packages into that
165+
specific environment. This enables you to import and use packages in your
181166
Python application.
182167

183168

184-
Leaving the virtual environment
185-
-------------------------------
169+
Deactivate a virtual environment
170+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186171

187-
If you want to switch projects or otherwise leave your virtual environment, simply run:
172+
If you want to switch projects or leave your virtual environment,
173+
``deactivate`` the environment:
188174

189175
.. code-block:: bash
190176
191177
deactivate
192178
193-
If you want to re-enter the virtual environment just follow the same instructions above
194-
about activating a virtual environment. There's no need to re-create the virtual environment.
179+
180+
Reactivate a virtual environment
181+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
182+
183+
If you want to reactivate an existing virtual environment, follow the same
184+
instructions about activating a virtual environment. There's no need to create
185+
a new virtual environment.
195186

196187

197-
Installing packages
198-
-------------------
188+
Install packages using pip
189+
--------------------------
199190

200-
Now that you're in your virtual environment you can install packages. Let's install the
191+
When your virtual environment is activated, you can install packages. Use the
192+
``pip install`` command to install packages.
193+
194+
Install a package
195+
~~~~~~~~~~~~~~~~~
196+
197+
For example,let's install the
201198
`Requests`_ library from the :term:`Python Package Index (PyPI)`:
202199

203200
.. tab:: Unix/macOS
@@ -232,8 +229,8 @@ pip should download requests and all of its dependencies and install them:
232229
.. _Requests: https://pypi.org/project/requests/
233230

234231

235-
Installing specific versions
236-
-----------------------------
232+
Install a specific package version
233+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237234

238235
pip allows you to specify which version of a package to install using
239236
:term:`version specifiers <Version Specifier>`. For example, to install
@@ -280,8 +277,8 @@ To install pre-release versions of packages, use the ``--pre`` flag:
280277
py -m pip install --pre requests
281278
282279
283-
Installing extras
284-
-----------------
280+
Install extras
281+
~~~~~~~~~~~~~~
285282

286283
Some packages have optional `extras`_. You can tell pip to install these by
287284
specifying the extra in brackets:
@@ -302,10 +299,11 @@ specifying the extra in brackets:
302299
https://setuptools.readthedocs.io/en/latest/userguide/dependency_management.html#optional-dependencies
303300

304301

305-
Installing from source
306-
----------------------
302+
Install a package from source
303+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
307304

308-
pip can install a package directly from source, for example:
305+
pip can install a package directly from its source code. For example, to install
306+
the source code in the ``google-auth`` directory:
309307

310308
.. tab:: Unix/macOS
311309

@@ -339,8 +337,8 @@ installed package without needing to re-install:
339337
py -m pip install --editable .
340338
341339
342-
Installing from version control systems
343-
---------------------------------------
340+
Install from version control systems
341+
------------------------------------
344342

345343
pip can install packages directly from their version control system. For
346344
example, you can install directly from a git repository:
@@ -353,8 +351,8 @@ For more information on supported version control systems and syntax, see pip's
353351
documentation on :ref:`VCS Support <pip:VCS Support>`.
354352

355353

356-
Installing from local archives
357-
------------------------------
354+
Install from local archives
355+
~~~~~~~~~~~~~~~~~~~~~~~~~~~
358356

359357
If you have a local copy of a :term:`Distribution Package`'s archive (a zip,
360358
wheel, or tar file) you can install it directly with pip:
@@ -392,8 +390,8 @@ connectivity or if you want to strictly control the origin of distribution
392390
packages.
393391

394392

395-
Using other package indexes
396-
---------------------------
393+
Install from other package indexes
394+
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
397395

398396
If you want to download packages from a different index than the
399397
:term:`Python Package Index (PyPI)`, you can use the ``--index-url`` flag:
@@ -444,8 +442,8 @@ install the latest version of ``requests`` and all of its dependencies:
444442
445443
py -m pip install --upgrade requests
446444
447-
Using requirements files
448-
------------------------
445+
Using a requirements file
446+
-------------------------
449447

450448
Instead of installing packages individually, pip allows you to declare all
451449
dependencies in a :ref:`Requirements File <pip:Requirements Files>`. For
@@ -504,5 +502,5 @@ Which will output a list of package specifiers such as:
504502
six==1.11.0
505503
urllib3==1.22
506504
507-
This is useful for creating :ref:`pip:Requirements Files` that can re-create
508-
the exact versions of all packages installed in an environment.
505+
The ``pip freeze`` command is useful for creating :ref:`pip:Requirements Files`
506+
that can re-create the exact versions of all packages installed in an environment.

0 commit comments

Comments
 (0)