pip_install.rst 34.1 KB
Newer Older
M
Marcus Smith 已提交
1 2
.. _`pip install`:

3
===========
M
Marcus Smith 已提交
4
pip install
5
===========
M
Marcus Smith 已提交
6

7 8
.. contents::

P
Pradyun Gedam 已提交
9

M
Marcus Smith 已提交
10
Usage
P
Pradyun Gedam 已提交
11
=====
M
Marcus Smith 已提交
12 13 14

.. pip-command-usage:: install

P
Pradyun Gedam 已提交
15

M
Marcus Smith 已提交
16
Description
P
Pradyun Gedam 已提交
17
===========
M
Marcus Smith 已提交
18 19 20

.. pip-command-description:: install

21
Overview
P
Pradyun Gedam 已提交
22
--------
23

P
Pradyun Gedam 已提交
24
pip install has several stages:
25

26 27 28 29 30 31 32
1. Identify the base requirements. The user supplied arguments are processed
   here.
2. Resolve dependencies. What will be installed is determined here.
3. Build wheels. All the dependencies that can be are built into wheels.
4. Install the packages (and uninstall anything being upgraded/replaced).

Argument Handling
P
Pradyun Gedam 已提交
33
-----------------
34 35 36 37 38 39 40 41 42

When looking at the items to be installed, pip checks what type of item
each is, in the following order:

1. Project or archive URL.
2. Local directory (which must contain a ``setup.py``, or pip will report
   an error).
3. Local file (a sdist or wheel format archive, following the naming
   conventions for those formats).
43
4. A requirement, as specified in :pep:`440`.
44 45 46 47 48

Each item identified is added to the set of requirements to be satisfied by
the install.

Working Out the Name and Version
P
Pradyun Gedam 已提交
49
--------------------------------
50 51 52 53 54 55 56 57 58 59

For each candidate item, pip needs to know the project name and version. For
wheels (identified by the ``.whl`` file extension) this can be obtained from
the filename, as per the Wheel spec. For local directories, or explicitly
specified sdist files, the ``setup.py egg_info`` command is used to determine
the project metadata. For sdists located via an index, the filename is parsed
for the name and project version (this is in theory slightly less reliable
than using the ``egg_info`` command, but avoids downloading and processing
unnecessary numbers of files).

60 61
Any URL may use the ``#egg=name`` syntax (see :ref:`VCS Support`) to
explicitly state the project name.
62 63

Satisfying Requirements
P
Pradyun Gedam 已提交
64
-----------------------
65 66 67 68 69 70 71

Once pip has the set of requirements to satisfy, it chooses which version of
each requirement to install using the simple rule that the latest version that
satisfies the given constraints will be installed (but see :ref:`here <Pre Release Versions>`
for an exception regarding pre-release versions). Where more than one source of
the chosen version is available, it is assumed that any source is acceptable
(as otherwise the versions would differ).
72

73
Installation Order
P
Pradyun Gedam 已提交
74
------------------
75

76
.. note::
77

78 79
   This section is only about installation order of runtime dependencies, and
   does not apply to build dependencies (those are specified using PEP 518).
80

81
As of v6.1.0, pip installs dependencies before their dependents, i.e. in
82
"topological order."  This is the only commitment pip currently makes related
83 84 85 86
to order.  While it may be coincidentally true that pip will install things in
the order of the install arguments or in the order of the items in a
requirements file, this is not a promise.

87 88
In the event of a dependency cycle (aka "circular dependency"), the current
implementation (which might possibly change later) has it such that the first
89
encountered member of the cycle is installed last.
90 91 92 93 94 95 96 97 98 99 100 101

For instance, if quux depends on foo which depends on bar which depends on baz,
which depends on foo::

    pip install quux
    ...
    Installing collected packages baz, bar, foo, quux

    pip install bar
    ...
    Installing collected packages foo, baz, bar

M
Marcus Smith 已提交
102

103 104 105 106 107 108 109
Prior to v6.1.0, pip made no commitments about install order.

The decision to install topologically is based on the principle that
installations should proceed in a way that leaves the environment usable at each
step. This has two main practical benefits:

1. Concurrent use of the environment during the install is more likely to work.
110 111 112
2. A failed install is less likely to leave a broken environment.  Although pip
   would like to support failure rollbacks eventually, in the mean time, this is
   an improvement.
113 114 115 116 117 118 119 120 121 122 123 124 125 126

Although the new install order is not intended to replace (and does not replace)
the use of ``setup_requires`` to declare build dependencies, it may help certain
projects install from sdist (that might previously fail) that fit the following
profile:

1. They have build dependencies that are also declared as install dependencies
   using ``install_requires``.
2. ``python setup.py egg_info`` works without their build dependencies being
   installed.
3. For whatever reason, they don't or won't declare their build dependencies using
   ``setup_requires``.


M
Marcus Smith 已提交
127 128
.. _`Requirements File Format`:

M
Marcus Smith 已提交
129
Requirements File Format
P
Pradyun Gedam 已提交
130
------------------------
M
Marcus Smith 已提交
131 132 133 134

Each line of the requirements file indicates something to be installed,
and like arguments to :ref:`pip install`, the following forms are supported::

135 136
    [[--option]...]
    <requirement specifier> [; markers] [[--option]...]
M
Marcus Smith 已提交
137 138 139 140
    <archive url/path>
    [-e] <local project path>
    [-e] <vcs project url>

141
For details on requirement specifiers, see :ref:`Requirement Specifiers`.
V
Victor Stinner 已提交
142

M
Marcus Smith 已提交
143 144
See the :ref:`pip install Examples<pip install Examples>` for examples of all these forms.

145 146 147
A line that begins with ``#`` is treated as a comment and ignored. Whitespace
followed by a ``#`` causes the ``#`` and the remainder of the line to be
treated as a comment.
M
Marcus Smith 已提交
148

149 150 151
A line ending in an unescaped ``\`` is treated as a line continuation
and the newline following it is effectively ignored.

152 153
Comments are stripped *before* line continuations are processed.

154 155 156
To interpret the requirements file in UTF-8 format add a comment
``# -*- coding: utf-8 -*-`` to the first or second line of the file.

157
The following options are supported:
M
Marcus Smith 已提交
158

159 160 161
  *  :ref:`-i, --index-url <install_--index-url>`
  *  :ref:`--extra-index-url <install_--extra-index-url>`
  *  :ref:`--no-index <install_--no-index>`
162 163 164
  *  :ref:`-c, --constraint <install_--constraint>`
  *  :ref:`-r, --requirement <install_--requirement>`
  *  :ref:`-e, --editable <install_--editable>`
165
  *  :ref:`-f, --find-links <install_--find-links>`
166 167
  *  :ref:`--no-binary <install_--no-binary>`
  *  :ref:`--only-binary <install_--only-binary>`
168
  *  :ref:`--require-hashes <install_--require-hashes>`
169
  *  :ref:`--pre <install_--pre>`
170
  *  :ref:`--trusted-host <--trusted-host>`
M
Marcus Smith 已提交
171

172 173
For example, to specify :ref:`--no-index <install_--no-index>` and two
:ref:`--find-links <install_--find-links>` locations:
M
Marcus Smith 已提交
174 175 176 177 178 179 180 181

::

--no-index
--find-links /my/local/archives
--find-links http://some.archives.com/archives


R
Robert Collins 已提交
182
If you wish, you can refer to other requirements files, like this::
M
Marcus Smith 已提交
183 184 185

    -r more_requirements.txt

186
You can also refer to :ref:`constraints files <Constraints Files>`, like this::
R
Robert Collins 已提交
187 188 189

    -c some_constraints.txt

190 191 192
.. _`Using Environment Variables`:

Using Environment Variables
P
Pradyun Gedam 已提交
193
^^^^^^^^^^^^^^^^^^^^^^^^^^^
194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

Since version 10, pip supports the use of environment variables inside the
requirements file. You can now store sensitive data (tokens, keys, etc.) in
environment variables and only specify the variable name for your requirements,
letting pip lookup the value at runtime. This approach aligns with the commonly
used `12-factor configuration pattern <https://12factor.net/config>`_.

You have to use the POSIX format for variable names including brackets around
the uppercase name as shown in this example: ``${API_TOKEN}``. pip will attempt
to find the corresponding environment variable defined on the host system at
runtime.

.. note::

   There is no support for other variable expansion syntaxes such as
   ``$VARIABLE`` and ``%VARIABLE%``.


D
Douglas Thor 已提交
212 213 214
.. _`Example Requirements File`:

Example Requirements File
P
Pradyun Gedam 已提交
215
^^^^^^^^^^^^^^^^^^^^^^^^^
D
Douglas Thor 已提交
216

217
Use ``pip install -r example-requirements.txt`` to install::
D
Douglas Thor 已提交
218 219

    #
220
    ####### example-requirements.txt #######
D
Douglas Thor 已提交
221
    #
222
    ###### Requirements without Version Specifiers ######
D
Douglas Thor 已提交
223 224 225 226
    nose
    nose-cov
    beautifulsoup4
    #
227
    ###### Requirements with Version Specifiers ######
D
Douglas Thor 已提交
228 229 230 231
    #   See https://www.python.org/dev/peps/pep-0440/#version-specifiers
    docopt == 0.6.1             # Version Matching. Must be version 0.6.1
    keyring >= 4.1.1            # Minimum version 4.1.1
    coverage != 3.5             # Version Exclusion. Anything except version 3.5
232
    Mopidy-Dirble ~= 1.1        # Compatible release. Same as >= 1.1, == 1.*
D
Douglas Thor 已提交
233 234 235 236 237
    #
    ###### Refer to other requirements files ######
    -r other-requirements.txt
    #
    #
D
Douglas Thor 已提交
238
    ###### A particular file ######
D
Douglas Thor 已提交
239 240
    ./downloads/numpy-1.9.2-cp34-none-win32.whl
    http://wxpython.org/Phoenix/snapshot-builds/wxPython_Phoenix-3.0.3.dev1820+49a8884-cp34-none-win_amd64.whl
241 242 243 244 245 246
    #
    ###### Additional Requirements without Version Specifiers ######
    #   Same as 1st section, just here to show that you can put things in any order.
    rejected
    green
    #
D
Douglas Thor 已提交
247

M
Marcus Smith 已提交
248 249 250
.. _`Requirement Specifiers`:

Requirement Specifiers
P
Pradyun Gedam 已提交
251
----------------------
M
Marcus Smith 已提交
252

253 254
pip supports installing from a package index using a :term:`requirement
specifier <pypug:Requirement Specifier>`. Generally speaking, a requirement
255
specifier is composed of a project name followed by optional :term:`version
256
specifiers <pypug:Version Specifier>`.  :pep:`508` contains a full specification
257 258
of the format of a requirement. Since version 18.1 pip supports the
``url_req``-form specification.
M
Marcus Smith 已提交
259

260
Some examples:
M
Marcus Smith 已提交
261 262 263

 ::

264
  SomeProject
265
  SomeProject == 1.3
J
jakirkham 已提交
266
  SomeProject >=1.2,<2.0
267 268 269
  SomeProject[foo, bar]
  SomeProject~=1.4.2

X
Xavier Fernandez 已提交
270
Since version 6.0, pip also supports specifiers containing `environment markers
271
<https://www.python.org/dev/peps/pep-0508/#environment-markers>`__ like so:
272 273 274

 ::

275
  SomeProject ==5.4 ; python_version < '2.7'
E
Emil Styrke 已提交
276
  SomeProject; sys_platform == 'win32'
277

278 279 280 281 282 283 284
Since version 19.1, pip also supports `direct references
<https://www.python.org/dev/peps/pep-0440/#direct-references>`__ like so:

 ::

  SomeProject @ file:///somewhere/...

285
Environment markers are supported in the command line and in requirements files.
M
Marcus Smith 已提交
286 287 288

.. note::

289 290
   Use quotes around specifiers in the shell when using ``>``, ``<``, or when
   using environment markers. Don't use quotes in requirement files. [1]_
M
Marcus Smith 已提交
291 292


293 294 295
.. _`Per-requirement Overrides`:

Per-requirement Overrides
P
Pradyun Gedam 已提交
296
-------------------------
297

298 299 300 301 302 303
Since version 7.0 pip supports controlling the command line options given to
``setup.py`` via requirements files. This disables the use of wheels (cached or
otherwise) for that package, as ``setup.py`` does not exist for wheels.

The ``--global-option`` and ``--install-option`` options are used to pass
options to ``setup.py``. For example:
304 305 306

 ::

307 308 309
    FooProject >= 1.2 --global-option="--no-user-cfg" \
                      --install-option="--prefix='/usr/local'" \
                      --install-option="--no-compile"
310 311 312 313 314 315

The above translates roughly into running FooProject's ``setup.py``
script as:

 ::

316 317
   python setup.py --no-user-cfg install --prefix='/usr/local' --no-compile

318 319 320 321 322 323 324 325 326 327 328
Note that the only way of giving more than one option to ``setup.py``
is through multiple ``--global-option`` and ``--install-option``
options, as shown in the example above. The value of each option is
passed as a single argument to the ``setup.py`` script. Therefore, a
line such as the following is invalid and would result in an
installation error.

::

   # Invalid. Please use '--install-option' twice as shown above.
   FooProject >= 1.2 --install-option="--prefix=/usr/local --no-compile"
M
Marcus Smith 已提交
329 330 331 332 333


.. _`Pre Release Versions`:

Pre-release Versions
P
Pradyun Gedam 已提交
334
--------------------
M
Marcus Smith 已提交
335 336

Starting with v1.4, pip will only install stable versions as specified by
337
`pre-releases`_ by default. If a version cannot be parsed as a compliant :pep:`440`
M
Marcus Smith 已提交
338 339 340 341 342 343 344
version then it is assumed to be a pre-release.

If a Requirement specifier includes a pre-release or development version
(e.g. ``>=0.0.dev0``) then pip will allow pre-release and development versions
for that requirement. This does not include the != flag.

The ``pip install`` command also supports a :ref:`--pre <install_--pre>` flag
345
that enables installation of pre-releases and development releases.
M
Marcus Smith 已提交
346 347


348
.. _pre-releases: https://www.python.org/dev/peps/pep-0440/#handling-of-pre-releases
M
Marcus Smith 已提交
349 350 351 352 353


.. _`VCS Support`:

VCS Support
P
Pradyun Gedam 已提交
354
-----------
M
Marcus Smith 已提交
355 356

pip supports installing from Git, Mercurial, Subversion and Bazaar, and detects
A
ABHYUDAY PRATAP SINGH 已提交
357
the type of VCS using URL prefixes: ``git+``, ``hg+``, ``svn+``, and ``bzr+``.
M
Marcus Smith 已提交
358

359 360
pip requires a working VCS command on your path: ``git``, ``hg``, ``svn``, or
``bzr``.
M
Marcus Smith 已提交
361 362 363 364

VCS projects can be installed in :ref:`editable mode <editable-installs>` (using
the :ref:`--editable <install_--editable>` option) or not.

365 366 367
* For editable installs, the clone location by default is ``<venv
  path>/src/SomeProject`` in virtual environments, and
  ``<cwd>/src/SomeProject``
M
Marcus Smith 已提交
368 369 370
  for global installs.  The :ref:`--src <install_--src>` option can be used to
  modify this location.
* For non-editable installs, the project is built locally in a temp dir and then
371
  installed normally. Note that if a satisfactory version of the package is
372 373 374 375
  already installed, the VCS source will not overwrite it without an
  ``--upgrade`` flag. VCS requirements pin the package version (specified
  in the ``setup.py`` file) of the target commit, not necessarily the commit
  itself.
X
Xavier Fernandez 已提交
376
* The :ref:`pip freeze` subcommand will record the VCS requirement specifier
377 378
  (referencing a specific commit) if and only if the install is done using the
  editable option.
M
Marcus Smith 已提交
379

A
ABHYUDAY PRATAP SINGH 已提交
380
The "project name" component of the URL suffix ``egg=<project name>``
381
is used by pip in its dependency logic to identify the project prior
382
to pip downloading and analyzing the metadata. For projects
383 384 385
where ``setup.py`` is not in the root of project, the "subdirectory" component
is used. The value of the "subdirectory" component should be a path starting
from the root of the project to where ``setup.py`` is located.
386

387 388 389 390 391 392 393 394 395 396 397
So if your repository layout is:

    - pkg_dir/

      - setup.py  # setup.py for package ``pkg``
      - some_module.py
    - other_dir/

      - some_file
    - some_other_file

398
You'll need to use ``pip install -e "vcs+protocol://repo_url/#egg=pkg&subdirectory=pkg_dir"``.
399

M
Marcus Smith 已提交
400 401

Git
P
Pradyun Gedam 已提交
402
^^^
M
Marcus Smith 已提交
403

404 405
pip currently supports cloning over ``git``, ``git+http``, ``git+https``,
``git+ssh``, ``git+git`` and ``git+file``:
M
Marcus Smith 已提交
406 407 408

Here are the supported forms::

409 410 411 412 413
    [-e] git://git.example.com/MyProject#egg=MyProject
    [-e] git+http://git.example.com/MyProject#egg=MyProject
    [-e] git+https://git.example.com/MyProject#egg=MyProject
    [-e] git+ssh://git.example.com/MyProject#egg=MyProject
    [-e] git+git://git.example.com/MyProject#egg=MyProject
C
Cooper Ry Lees 已提交
414
    [-e] git+file:///home/user/projects/MyProject#egg=MyProject
M
Marcus Smith 已提交
415

416
Passing a branch name, a commit hash, a tag name or a git ref is possible like so::
M
Marcus Smith 已提交
417

418 419 420
    [-e] git://git.example.com/MyProject.git@master#egg=MyProject
    [-e] git://git.example.com/MyProject.git@v1.0#egg=MyProject
    [-e] git://git.example.com/MyProject.git@da39a3ee5e6b4b0d3255bfef95601890afd80709#egg=MyProject
421
    [-e] git://git.example.com/MyProject.git@refs/pull/123/head#egg=MyProject
M
Marcus Smith 已提交
422

423 424 425 426
When passing a commit hash, specifying a full hash is preferable to a partial
hash because a full hash allows pip to operate more efficiently (e.g. by
making fewer network calls).

M
Marcus Smith 已提交
427
Mercurial
P
Pradyun Gedam 已提交
428
^^^^^^^^^
M
Marcus Smith 已提交
429

C
Chris Hunt 已提交
430 431
The supported schemes are: ``hg+file``, ``hg+http``, ``hg+https``,
``hg+static-http``, and ``hg+ssh``.
M
Marcus Smith 已提交
432 433 434

Here are the supported forms::

435 436 437 438
    [-e] hg+http://hg.myproject.org/MyProject#egg=MyProject
    [-e] hg+https://hg.myproject.org/MyProject#egg=MyProject
    [-e] hg+ssh://hg.myproject.org/MyProject#egg=MyProject
    [-e] hg+file:///home/user/projects/MyProject#egg=MyProject
M
Marcus Smith 已提交
439 440 441 442

You can also specify a revision number, a revision hash, a tag name or a local
branch name like so::

443 444 445 446
    [-e] hg+http://hg.example.com/MyProject@da39a3ee5e6b#egg=MyProject
    [-e] hg+http://hg.example.com/MyProject@2019#egg=MyProject
    [-e] hg+http://hg.example.com/MyProject@v1.0#egg=MyProject
    [-e] hg+http://hg.example.com/MyProject@special_feature#egg=MyProject
M
Marcus Smith 已提交
447 448

Subversion
P
Pradyun Gedam 已提交
449
^^^^^^^^^^
M
Marcus Smith 已提交
450 451 452

pip supports the URL schemes ``svn``, ``svn+svn``, ``svn+http``, ``svn+https``, ``svn+ssh``.

453 454 455 456 457 458
Here are some of the supported forms::

    [-e] svn+https://svn.example.com/MyProject#egg=MyProject
    [-e] svn+ssh://svn.example.com/MyProject#egg=MyProject
    [-e] svn+ssh://user@svn.example.com/MyProject#egg=MyProject

M
Marcus Smith 已提交
459 460
You can also give specific revisions to an SVN URL, like so::

461 462
    [-e] svn+svn://svn.example.com/svn/MyProject#egg=MyProject
    [-e] svn+http://svn.example.com/svn/MyProject/trunk@2019#egg=MyProject
M
Marcus Smith 已提交
463 464 465 466 467 468

which will check out revision 2019.  ``@{20080101}`` would also check
out the revision from 2008-01-01. You can only check out specific
revisions using ``-e svn+...``.

Bazaar
P
Pradyun Gedam 已提交
469
^^^^^^
M
Marcus Smith 已提交
470 471 472 473 474 475

pip supports Bazaar using the ``bzr+http``, ``bzr+https``, ``bzr+ssh``,
``bzr+sftp``, ``bzr+ftp`` and ``bzr+lp`` schemes.

Here are the supported forms::

476 477 478 479
    [-e] bzr+http://bzr.example.com/MyProject/trunk#egg=MyProject
    [-e] bzr+sftp://user@example.com/MyProject/trunk#egg=MyProject
    [-e] bzr+ssh://user@example.com/MyProject/trunk#egg=MyProject
    [-e] bzr+ftp://user@example.com/MyProject/trunk#egg=MyProject
M
Marcus Smith 已提交
480 481 482 483
    [-e] bzr+lp:MyProject#egg=MyProject

Tags or revisions can be installed like so::

484 485
    [-e] bzr+https://bzr.example.com/MyProject/trunk@2019#egg=MyProject
    [-e] bzr+http://bzr.example.com/MyProject/trunk@v1.0#egg=MyProject
M
Marcus Smith 已提交
486

487
Using Environment Variables
P
Pradyun Gedam 已提交
488
^^^^^^^^^^^^^^^^^^^^^^^^^^^
489 490 491 492 493 494 495 496 497 498 499 500 501

Since version 10, pip also makes it possible to use environment variables which
makes it possible to reference private repositories without having to store
access tokens in the requirements file. For example, a private git repository
allowing Basic Auth for authentication can be refenced like this::

    [-e] git+http://${AUTH_USER}:${AUTH_PASSWORD}@git.example.com/MyProject#egg=MyProject
    [-e] git+https://${AUTH_USER}:${AUTH_PASSWORD}@git.example.com/MyProject#egg=MyProject

.. note::

   Only ``${VARIABLE}`` is supported, other formats like ``$VARIABLE`` or
   ``%VARIABLE%`` won't work.
M
Marcus Smith 已提交
502 503

Finding Packages
P
Pradyun Gedam 已提交
504
----------------
M
Marcus Smith 已提交
505 506

pip searches for packages on `PyPI`_ using the
507
`HTTP simple interface <https://pypi.org/simple/>`_,
508
which is documented `here <https://setuptools.readthedocs.io/en/latest/easy_install.html#package-index-api>`_
509
and `there <https://www.python.org/dev/peps/pep-0503/>`_.
M
Marcus Smith 已提交
510

511 512
pip offers a number of package index options for modifying how packages are
found.
M
Marcus Smith 已提交
513

514 515 516 517
pip looks for packages in a number of places: on PyPI (if not disabled via
``--no-index``), in the local filesystem, and in any additional repositories
specified via ``--find-links`` or ``--index-url``. There is no ordering in
the locations that are searched. Rather they are all checked, and the "best"
518
match for the requirements (in terms of version number - see :pep:`440` for
519 520
details) is selected.

M
Marcus Smith 已提交
521 522 523 524 525 526
See the :ref:`pip install Examples<pip install Examples>`.


.. _`SSL Certificate Verification`:

SSL Certificate Verification
P
Pradyun Gedam 已提交
527
----------------------------
M
Marcus Smith 已提交
528

E
Erik Rose 已提交
529 530
Starting with v1.3, pip provides SSL certificate verification over https, to
prevent man-in-the-middle attacks against PyPI downloads.
M
Marcus Smith 已提交
531 532


533 534 535
.. _`Caching`:

Caching
P
Pradyun Gedam 已提交
536
-------
537

E
Erik Rose 已提交
538
Starting with v6.0, pip provides an on-by-default cache which functions
539 540 541 542
similarly to that of a web browser. While the cache is on by default and is
designed do the right thing by default you can disable the cache and always
access PyPI by utilizing the ``--no-cache-dir`` option.

T
tim smith 已提交
543
When making any HTTP request pip will first check its local cache to determine
544 545 546 547 548 549 550 551 552
if it has a suitable response stored for that request which has not expired. If
it does then it simply returns that response and doesn't make the request.

If it has a response stored, but it has expired, then it will attempt to make a
conditional request to refresh the cache which will either return an empty
response telling pip to simply use the cached item (and refresh the expiration
timer) or it will return a whole new response which pip can then store in the
cache.

T
tim smith 已提交
553
When storing items in the cache, pip will respect the ``CacheControl`` header
554 555 556 557 558
if it exists, or it will fall back to the ``Expires`` header if that exists.
This allows pip to function as a browser would, and allows the index server
to communicate to pip how long it is reasonable to cache any particular item.

While this cache attempts to minimize network activity, it does not prevent
559 560
network access altogether. If you want a local install solution that
circumvents accessing PyPI, see :ref:`Installing from local packages`.
561

562 563 564 565
The default location for the cache directory depends on the Operating System:

Unix
  :file:`~/.cache/pip` and it respects the ``XDG_CACHE_HOME`` directory.
L
Lipis 已提交
566
macOS
567 568
  :file:`~/Library/Caches/pip`.
Windows
569
  :file:`<CSIDL_LOCAL_APPDATA>\\pip\\Cache`
570

571

572 573
.. _`Wheel cache`:

574
Wheel Cache
P
Pradyun Gedam 已提交
575
^^^^^^^^^^^
576

P
Pradyun Gedam 已提交
577
pip will read from the subdirectory ``wheels`` within the pip cache directory
578 579 580 581
and use any packages found there. This is disabled via the same
``--no-cache-dir`` option that disables the HTTP cache. The internal structure
of that is not part of the pip API. As of 7.0, pip makes a subdirectory for
each sdist that wheels are built from and places the resulting wheels inside.
582

583 584 585
As of version 20.0, pip also caches wheels when building from an immutable Git
reference (i.e. a commit hash).

P
Pradyun Gedam 已提交
586
pip attempts to choose the best wheels from those built in preference to
587
building a new wheel. Note that this means when a package has both optional
588
C extensions and builds ``py`` tagged wheels when the C extension can't be built
T
tim smith 已提交
589
that pip will not attempt to build a better wheel for Pythons that would have
590
supported it, once any generic wheel is built. To correct this, make sure that
X
Xavier Fernandez 已提交
591
the wheels are built with Python specific tags - e.g. pp on PyPy.
592

593 594 595
When no wheels are found for an sdist, pip will attempt to build a wheel
automatically and insert it into the wheel cache.

596

E
Erik Rose 已提交
597
.. _`hash-checking mode`:
M
Marcus Smith 已提交
598

E
Erik Rose 已提交
599
Hash-Checking Mode
P
Pradyun Gedam 已提交
600
------------------
M
Marcus Smith 已提交
601

E
Erik Rose 已提交
602 603 604 605
Since version 8.0, pip can check downloaded package archives against local
hashes to protect against remote tampering. To verify a package against one or
more hashes, add them to the end of the line::

606 607
    FooProject == 1.2 --hash=sha256:2cf24dba5fb0a30e26e83b2ac5b9e29e1b161e5c1fa7425e73043362938b9824 \
                      --hash=sha256:486ea46224d1bb4fb680f34f7c9ad96a8f24ec88be73ea8e5a6c65260e9cb8a7
E
Erik Rose 已提交
608 609 610 611 612 613

(The ability to use multiple hashes is important when a package has both
binary and source distributions or when it offers binary distributions for a
variety of platforms.)

The recommended hash algorithm at the moment is sha256, but stronger ones are
E
Erik Rose 已提交
614
allowed, including all those supported by ``hashlib``. However, weaker ones
615
such as md5, sha1, and sha224 are excluded to avoid giving a false sense of
E
Erik Rose 已提交
616 617 618
security.

Hash verification is an all-or-nothing proposition. Specifying a ``--hash``
619
against any requirement not only checks that hash but also activates a global
E
Erik Rose 已提交
620 621 622 623 624 625 626
*hash-checking mode*, which imposes several other security restrictions:

* Hashes are required for all requirements. This is because a partially-hashed
  requirements file is of little use and thus likely an error: a malicious
  actor could slip bad code into the installation via one of the unhashed
  requirements. Note that hashes embedded in URL-style requirements via the
  ``#md5=...`` syntax suffice to satisfy this rule (regardless of hash
E
Erik Rose 已提交
627 628
  strength, for legacy reasons), though you should use a stronger
  hash like sha256 whenever possible.
629
* Hashes are required for all dependencies. An error results if there is a
E
Erik Rose 已提交
630 631 632 633 634 635 636 637
  dependency that is not spelled out and hashed in the requirements file.
* Requirements that take the form of project names (rather than URLs or local
  filesystem paths) must be pinned to a specific version using ``==``. This
  prevents a surprising hash mismatch upon the release of a new version
  that matches the requirement specifier.
* ``--egg`` is disallowed, because it delegates installation of dependencies
  to setuptools, giving up pip's ability to enforce any of the above.

638 639
.. _`--require-hashes`:

E
Erik Rose 已提交
640
Hash-checking mode can be forced on with the ``--require-hashes`` command-line
E
Erik Rose 已提交
641 642 643 644 645 646 647 648 649 650 651 652 653
option::

    $ pip install --require-hashes -r requirements.txt
        ...
        Hashes are required in --require-hashes mode (implicitly on when a hash is
        specified for any package). These requirements were missing hashes,
        leaving them open to tampering. These are the hashes the downloaded
        archives actually had. You can add lines like these to your requirements
        files to prevent tampering.
            pyelasticsearch==1.0 --hash=sha256:44ddfb1225054d7d6b1d02e9338e7d4809be94edbe9929a2ec0807d38df993fa
            more-itertools==2.2 --hash=sha256:93e62e05c7ad3da1a233def6731e8285156701e3419a5fe279017c429ec67ce0

This can be useful in deploy scripts, to ensure that the author of the
E
Erik Rose 已提交
654
requirements file provided hashes. It is also a convenient way to bootstrap
E
Erik Rose 已提交
655 656 657 658 659
your list of hashes, since it shows the hashes of the packages it fetched. It
fetches only the preferred archive for each package, so you may still need to
add hashes for alternatives archives using :ref:`pip hash`: for instance if
there is both a binary and a source distribution.

660 661 662 663 664 665 666
The :ref:`wheel cache <Wheel cache>` is disabled in hash-checking mode to
prevent spurious hash mismatch errors. These would otherwise occur while
installing sdists that had already been automatically built into cached wheels:
those wheels would be selected for installation, but their hashes would not
match the sdist ones from the requirements file. A further complication is that
locally built wheels are nondeterministic: contemporary modification times make
their way into the archive, making hashes unpredictable across machines and
667 668 669 670
cache flushes. Compilation of C code adds further nondeterminism, as many
compilers include random-seeded values in their output. However, wheels fetched
from index servers are the same every time. They land in pip's HTTP cache, not
its wheel cache, and are used normally in hash-checking mode. The only downside
X
Xavier Fernandez 已提交
671
of having the wheel cache disabled is thus extra build time for sdists, and
672 673
this can be solved by making sure pre-built wheels are available from the index
server.
674 675 676 677

Hash-checking mode also works with :ref:`pip download` and :ref:`pip wheel`. A
:ref:`comparison of hash-checking mode with other repeatability strategies
<Repeatability>` is available in the User Guide.
E
Erik Rose 已提交
678 679

.. warning::
680

E
Erik Rose 已提交
681 682 683 684 685 686
    Beware of the ``setup_requires`` keyword arg in :file:`setup.py`. The
    (rare) packages that use it will cause those dependencies to be downloaded
    by setuptools directly, skipping pip's hash-checking. If you need to use
    such a package, see :ref:`Controlling
    setup_requires<controlling-setup-requires>`.

687
.. warning::
688

689
    Be careful not to nullify all your security work when you install your
690 691 692
    actual project by using setuptools directly: for example, by calling
    ``python setup.py install``, ``python setup.py develop``, or
    ``easy_install``. Setuptools will happily go out and download, unchecked,
693
    anything you missed in your requirements file—and it’s easy to miss things
694 695
    as your project evolves. To be safe, install your project using pip and
    :ref:`--no-deps <install_--no-deps>`.
696

697 698 699 700 701 702 703
    Instead of ``python setup.py develop``, use... ::

        pip install --no-deps -e .

    Instead of ``python setup.py install``, use... ::

        pip install --no-deps .
704

E
Erik Rose 已提交
705 706

Hashes from PyPI
P
Pradyun Gedam 已提交
707
^^^^^^^^^^^^^^^^
E
Erik Rose 已提交
708

709 710 711 712 713 714 715
PyPI provides an MD5 hash in the fragment portion of each package download URL,
like ``#md5=123...``, which pip checks as a protection against download
corruption. Other hash algorithms that have guaranteed support from ``hashlib``
are also supported here: sha1, sha224, sha384, sha256, and sha512. Since this
hash originates remotely, it is not a useful guard against tampering and thus
does not satisfy the ``--require-hashes`` demand that every package have a
local hash.
M
Marcus Smith 已提交
716 717


O
Omry Yadan 已提交
718
Local project installs
P
Pradyun Gedam 已提交
719
----------------------
O
Omry Yadan 已提交
720 721 722 723 724
pip supports installing local project in both regular mode and editable mode.
You can install local projects by specifying the project path to pip::

$ pip install path/to/SomeProject

O
Omry Yadan 已提交
725
During regular installation, pip will copy the entire project directory to a temporary location and install from there.
O
Omry Yadan 已提交
726 727 728
The exception is that pip will exclude .tox and .nox directories present in the top level of the project from being copied.


M
Marcus Smith 已提交
729 730 731
.. _`editable-installs`:

"Editable" Installs
P
Pradyun Gedam 已提交
732
^^^^^^^^^^^^^^^^^^^
M
Marcus Smith 已提交
733 734

"Editable" installs are fundamentally `"setuptools develop mode"
735
<https://setuptools.readthedocs.io/en/latest/setuptools.html#development-mode>`_
M
Marcus Smith 已提交
736 737 738 739 740 741 742
installs.

You can install local projects or VCS projects in "editable" mode::

$ pip install -e path/to/SomeProject
$ pip install -e git+http://repo/my_project.git#egg=SomeProject

D
Daniele Procida 已提交
743 744
(See the :ref:`VCS Support` section above for more information on VCS-related syntax.)

M
Marcus Smith 已提交
745 746 747 748 749
For local projects, the "SomeProject.egg-info" directory is created relative to
the project path.  This is one advantage over just using ``setup.py develop``,
which creates the "egg-info" directly relative the current working directory.


750
.. _`controlling-setup-requires`:
M
Marcus Smith 已提交
751 752

Controlling setup_requires
P
Pradyun Gedam 已提交
753
--------------------------
M
Marcus Smith 已提交
754 755

Setuptools offers the ``setup_requires`` `setup() keyword
756
<https://setuptools.readthedocs.io/en/latest/setuptools.html#new-and-changed-setup-keywords>`_
757 758 759
for specifying dependencies that need to be present in order for the
``setup.py`` script to run.  Internally, Setuptools uses ``easy_install``
to fulfill these dependencies.
M
Marcus Smith 已提交
760 761

pip has no way to control how these dependencies are located.  None of the
762
package index options have an effect.
M
Marcus Smith 已提交
763 764 765

The solution is to configure a "system" or "personal" `Distutils configuration
file
766
<https://docs.python.org/3/install/index.html#distutils-configuration-files>`_ to
M
Marcus Smith 已提交
767 768 769 770 771 772 773 774 775 776 777 778 779 780 781
manage the fulfillment.

For example, to have the dependency located at an alternate index, add this:

::

  [easy_install]
  index_url = https://my.index-mirror.com

To have the dependency located from a local directory and not crawl PyPI, add this:

::

  [easy_install]
  allow_hosts = ''
782
  find_links = file:///path/to/local/archives/
M
Marcus Smith 已提交
783 784


785
Build System Interface
P
Pradyun Gedam 已提交
786
----------------------
787 788 789 790 791 792 793 794 795

In order for pip to install a package from source, ``setup.py`` must implement
the following commands::

    setup.py egg_info [--egg-base XXX]
    setup.py install --record XXX [--single-version-externally-managed] [--root XXX] [--compile|--no-compile] [--install-headers XXX]

The ``egg_info`` command should create egg metadata for the package, as
described in the setuptools documentation at
796
https://setuptools.readthedocs.io/en/latest/setuptools.html#egg-info-create-egg-metadata-and-set-build-tags
797 798 799 800 801 802 803 804 805 806 807 808

The ``install`` command should implement the complete process of installing the
package to the target directory XXX.

To install a package in "editable" mode (``pip install -e``), ``setup.py`` must
implement the following command::

    setup.py develop --no-deps

This should implement the complete process of installing the package in
"editable" mode.

809 810 811 812
All packages will be attempted to built into wheels::

    setup.py bdist_wheel -d XXX

813 814 815 816 817 818 819 820 821 822 823
One further ``setup.py`` command is invoked by ``pip install``::

    setup.py clean

This command is invoked to clean up temporary commands from the build. (TODO:
Investigate in more detail when this command is required).

No other build system commands are invoked by the ``pip install`` command.

Installing a package from a wheel does not invoke the build system at all.

824
.. _PyPI: https://pypi.org/
825
.. _setuptools extras: https://setuptools.readthedocs.io/en/latest/setuptools.html#declaring-extras-optional-features-with-their-own-dependencies
826 827 828



M
Marcus Smith 已提交
829
.. _`pip install Options`:
M
Marcus Smith 已提交
830

P
Pradyun Gedam 已提交
831

M
Marcus Smith 已提交
832
Options
P
Pradyun Gedam 已提交
833
=======
M
Marcus Smith 已提交
834 835 836

.. pip-command-options:: install

837
.. pip-index-options:: install
M
Marcus Smith 已提交
838 839 840 841


.. _`pip install Examples`:

P
Pradyun Gedam 已提交
842

M
Marcus Smith 已提交
843
Examples
P
Pradyun Gedam 已提交
844
========
M
Marcus Smith 已提交
845

846
#. Install ``SomePackage`` and its dependencies from `PyPI`_ using :ref:`Requirement Specifiers`
M
Marcus Smith 已提交
847

848
    ::
M
Marcus Smith 已提交
849

850 851 852
      $ pip install SomePackage            # latest version
      $ pip install SomePackage==1.0.4     # specific version
      $ pip install 'SomePackage>=1.0.4'     # minimum version
M
Marcus Smith 已提交
853 854


855
#. Install a list of requirements specified in a file.  See the :ref:`Requirements files <Requirements Files>`.
M
Marcus Smith 已提交
856

857
    ::
M
Marcus Smith 已提交
858

859
      $ pip install -r requirements.txt
M
Marcus Smith 已提交
860 861


862
#. Upgrade an already installed ``SomePackage`` to the latest from PyPI.
M
Marcus Smith 已提交
863

864
    ::
M
Marcus Smith 已提交
865

866
      $ pip install --upgrade SomePackage
M
Marcus Smith 已提交
867 868


869
#. Install a local project in "editable" mode. See the section on :ref:`Editable Installs <editable-installs>`.
M
Marcus Smith 已提交
870

871
    ::
M
Marcus Smith 已提交
872

873 874
      $ pip install -e .                     # project in current directory
      $ pip install -e path/to/project       # project in another directory
M
Marcus Smith 已提交
875 876


877
#. Install a project from VCS in "editable" mode. See the sections on :ref:`VCS Support <VCS Support>` and :ref:`Editable Installs <editable-installs>`.
M
Marcus Smith 已提交
878

879
    ::
M
Marcus Smith 已提交
880

881 882 883 884 885
      $ pip install -e git+https://git.repo/some_pkg.git#egg=SomePackage          # from git
      $ pip install -e hg+https://hg.repo/some_pkg.git#egg=SomePackage            # from mercurial
      $ pip install -e svn+svn://svn.repo/some_pkg/trunk/#egg=SomePackage         # from svn
      $ pip install -e git+https://git.repo/some_pkg.git@feature#egg=SomePackage  # from 'feature' branch
      $ pip install -e "git+https://git.repo/some_repo.git#egg=subdir&subdirectory=subdir_path" # install a python package from a repo subdirectory
M
Marcus Smith 已提交
886

887
#. Install a package with `setuptools extras`_.
M
Marcus Smith 已提交
888

889
    ::
M
Marcus Smith 已提交
890

891
      $ pip install SomePackage[PDF]
C
Cody 已提交
892
      $ pip install git+https://git.repo/some_pkg.git#egg=SomePackage[PDF]
893
      $ pip install .[PDF]  # project in current directory
894
      $ pip install SomePackage[PDF]==3.0
895
      $ pip install SomePackage[PDF,EPUB]  # multiple extras
M
Marcus Smith 已提交
896 897


898
#. Install a particular source archive file.
M
Marcus Smith 已提交
899

900
    ::
M
Marcus Smith 已提交
901

902 903
      $ pip install ./downloads/SomePackage-1.0.4.tar.gz
      $ pip install http://my.package.repo/SomePackage-1.0.4.zip
M
Marcus Smith 已提交
904 905


906 907 908 909 910 911 912 913
#. Install a particular source archive file following :pep:`440` direct references.

    ::

      $ pip install SomeProject==1.0.4@http://my.package.repo//SomeProject-1.2.3-py33-none-any.whl
      $ pip install "SomeProject==1.0.4 @ http://my.package.repo//SomeProject-1.2.3-py33-none-any.whl"


914
#. Install from alternative package repositories.
M
Marcus Smith 已提交
915

916
   Install from a different index, and not `PyPI`_ ::
M
Marcus Smith 已提交
917

918
     $ pip install --index-url http://my.package.repo/simple/ SomePackage
M
Marcus Smith 已提交
919

920
   Search an additional index during install, in addition to `PyPI`_ ::
M
Marcus Smith 已提交
921

922
     $ pip install --extra-index-url http://my.package.repo/simple SomePackage
M
Marcus Smith 已提交
923

924
   Install from a local flat directory containing archives (and don't scan indexes)::
M
Marcus Smith 已提交
925

926 927 928
     $ pip install --no-index --find-links=file:///local/dir/ SomePackage
     $ pip install --no-index --find-links=/local/dir/ SomePackage
     $ pip install --no-index --find-links=relative/dir/ SomePackage
M
Marcus Smith 已提交
929 930


931
#. Find pre-release and development versions, in addition to stable versions.  By default, pip only finds stable versions.
M
Marcus Smith 已提交
932

933
    ::
M
Marcus Smith 已提交
934

935
      $ pip install --pre SomePackage
936 937 938 939 940

----

.. [1] This is true with the exception that pip v7.0 and v7.0.1 required quotes
       around specifiers containing environment markers in requirement files.