1
- Installing packages using pip and virtual environments
2
- ======================================================
1
+ Installing packages using pip and venv
2
+ ======================================
3
3
4
4
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:
8
7
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
12
12
13
13
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 `.
16
16
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.
20
30
21
31
22
32
.. tab :: Unix/macOS
@@ -59,94 +69,68 @@ installed.
59
69
pip 21.1.3 from c:\python39\lib\site-packages (Python 3.9.4)
60
70
61
71
72
+ Create and Use Virtual Environments
73
+ -----------------------------------
62
74
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
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
76
77
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.
77
84
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.
103
88
104
89
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 ``.
107
91
108
92
.. tab :: Unix/macOS
109
93
110
94
.. code-block :: bash
111
95
112
- python3 -m venv env
96
+ python3 -m venv .venv
113
97
114
98
.. tab :: Windows
115
99
116
100
.. code-block :: bat
117
101
118
- py -m venv env
102
+ py -m venv .venv
119
103
120
104
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 ``.
122
106
123
- venv will create a virtual Python installation in the ``env `` folder.
107
+ `` venv `` will create a virtual Python installation in the ``.venv `` folder.
124
108
125
109
.. Note :: You should exclude your virtual environment directory from your version
126
110
control system using ``.gitignore `` or similar.
127
111
128
112
129
- Activating a virtual environment
130
- --------------------------------
113
+ Activate a virtual environment
114
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
131
115
132
116
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 ``.
136
120
137
121
.. tab :: Unix/macOS
138
122
139
123
.. code-block :: bash
140
124
141
- source env /bin/activate
125
+ source .venv /bin/activate
142
126
143
127
.. tab :: Windows
144
128
145
129
.. code-block :: bat
146
130
147
- .\env \Scripts\activate
131
+ .\.venv \Scripts\activate
148
132
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
150
134
Python interpreter:
151
135
152
136
.. tab :: Unix/macOS
@@ -161,43 +145,56 @@ Python interpreter:
161
145
162
146
where python
163
147
164
- It should be in the ``env `` directory:
148
+ When the virtual environment is activated, the location will include
149
+ the ``.venv `` directory:
165
150
166
151
.. tab :: Unix/macOS
167
152
168
153
.. code-block :: bash
169
154
170
- .../env /bin/python
155
+ .../.venv /bin/python
171
156
172
157
.. tab :: Windows
173
158
174
159
.. code-block :: bat
175
160
176
- ...\env \Scripts\python.exe
161
+ ...\.venv \Scripts\python.exe
177
162
178
163
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
181
166
Python application.
182
167
183
168
184
- Leaving the virtual environment
185
- -------------------------------
169
+ Deactivate a virtual environment
170
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
186
171
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:
188
174
189
175
.. code-block :: bash
190
176
191
177
deactivate
192
178
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.
195
186
196
187
197
- Installing packages
198
- -------------------
188
+ Install packages using pip
189
+ --------------------------
199
190
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
201
198
`Requests `_ library from the :term: `Python Package Index (PyPI) `:
202
199
203
200
.. tab :: Unix/macOS
@@ -232,8 +229,8 @@ pip should download requests and all of its dependencies and install them:
232
229
.. _Requests : https://pypi.org/project/requests/
233
230
234
231
235
- Installing specific versions
236
- -----------------------------
232
+ Install a specific package version
233
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
237
234
238
235
pip allows you to specify which version of a package to install using
239
236
:term: `version specifiers <Version Specifier> `. For example, to install
@@ -280,8 +277,8 @@ To install pre-release versions of packages, use the ``--pre`` flag:
280
277
py -m pip install --pre requests
281
278
282
279
283
- Installing extras
284
- -----------------
280
+ Install extras
281
+ ~~~~~~~~~~~~~~
285
282
286
283
Some packages have optional `extras `_. You can tell pip to install these by
287
284
specifying the extra in brackets:
@@ -302,10 +299,11 @@ specifying the extra in brackets:
302
299
https://setuptools.readthedocs.io/en/latest/userguide/dependency_management.html#optional-dependencies
303
300
304
301
305
- Installing from source
306
- ----------------------
302
+ Install a package from source
303
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
307
304
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:
309
307
310
308
.. tab :: Unix/macOS
311
309
@@ -339,8 +337,8 @@ installed package without needing to re-install:
339
337
py -m pip install --editable .
340
338
341
339
342
- Installing from version control systems
343
- ---------------------------------------
340
+ Install from version control systems
341
+ ------------------------------------
344
342
345
343
pip can install packages directly from their version control system. For
346
344
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
353
351
documentation on :ref: `VCS Support <pip:VCS Support >`.
354
352
355
353
356
- Installing from local archives
357
- ------------------------------
354
+ Install from local archives
355
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~
358
356
359
357
If you have a local copy of a :term: `Distribution Package `'s archive (a zip,
360
358
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
392
390
packages.
393
391
394
392
395
- Using other package indexes
396
- ---------------------------
393
+ Install from other package indexes
394
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
397
395
398
396
If you want to download packages from a different index than the
399
397
: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:
444
442
445
443
py -m pip install --upgrade requests
446
444
447
- Using requirements files
448
- ------------------------
445
+ Using a requirements file
446
+ -------------------------
449
447
450
448
Instead of installing packages individually, pip allows you to declare all
451
449
dependencies in a :ref: `Requirements File <pip:Requirements Files >`. For
@@ -504,5 +502,5 @@ Which will output a list of package specifiers such as:
504
502
six==1.11.0
505
503
urllib3==1.22
506
504
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