Skip to content

Commit 498d800

Browse files
authored
Merge pull request #10054 from pradyunsg/topic/caching
2 parents 9e1a92e + d63f06a commit 498d800

File tree

3 files changed

+89
-50
lines changed

3 files changed

+89
-50
lines changed

docs/html/cli/pip_install.rst

Lines changed: 2 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -574,62 +574,14 @@ overridden by using ``--cert`` option or by using ``PIP_CERT``,
574574
Caching
575575
-------
576576

577-
Starting with v6.0, pip provides an on-by-default cache which functions
578-
similarly to that of a web browser. While the cache is on by default and is
579-
designed do the right thing by default you can disable the cache and always
580-
access PyPI by utilizing the ``--no-cache-dir`` option.
581-
582-
When making any HTTP request pip will first check its local cache to determine
583-
if it has a suitable response stored for that request which has not expired. If
584-
it does then it simply returns that response and doesn't make the request.
585-
586-
If it has a response stored, but it has expired, then it will attempt to make a
587-
conditional request to refresh the cache which will either return an empty
588-
response telling pip to simply use the cached item (and refresh the expiration
589-
timer) or it will return a whole new response which pip can then store in the
590-
cache.
591-
592-
While this cache attempts to minimize network activity, it does not prevent
593-
network access altogether. If you want a local install solution that
594-
circumvents accessing PyPI, see :ref:`Installing from local packages`.
595-
596-
The default location for the cache directory depends on the operating system:
597-
598-
Unix
599-
:file:`~/.cache/pip` and it respects the ``XDG_CACHE_HOME`` directory.
600-
macOS
601-
:file:`~/Library/Caches/pip`.
602-
Windows
603-
:file:`<CSIDL_LOCAL_APPDATA>\\pip\\Cache`
604-
605-
Run ``pip cache dir`` to show the cache directory and see :ref:`pip cache` to
606-
inspect and manage pip’s cache.
607-
577+
This is now covered in :doc:`../topics/caching`
608578

609579
.. _`Wheel cache`:
610580

611581
Wheel Cache
612582
^^^^^^^^^^^
613583

614-
pip will read from the subdirectory ``wheels`` within the pip cache directory
615-
and use any packages found there. This is disabled via the same
616-
``--no-cache-dir`` option that disables the HTTP cache. The internal structure
617-
of that is not part of the pip API. As of 7.0, pip makes a subdirectory for
618-
each sdist that wheels are built from and places the resulting wheels inside.
619-
620-
As of version 20.0, pip also caches wheels when building from an immutable Git
621-
reference (i.e. a commit hash).
622-
623-
pip attempts to choose the best wheels from those built in preference to
624-
building a new wheel. Note that this means when a package has both optional
625-
C extensions and builds ``py`` tagged wheels when the C extension can't be built
626-
that pip will not attempt to build a better wheel for Pythons that would have
627-
supported it, once any generic wheel is built. To correct this, make sure that
628-
the wheels are built with Python specific tags - e.g. pp on PyPy.
629-
630-
When no wheels are found for an sdist, pip will attempt to build a wheel
631-
automatically and insert it into the wheel cache.
632-
584+
This is now covered in :doc:`../topics/caching`
633585

634586
.. _`hash-checking mode`:
635587

docs/html/topics/caching.md

Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
# Caching
2+
3+
```{versionadded} 6.0
4+
```
5+
6+
pip provides an on-by-default caching, designed to reduce the amount of time
7+
spent on duplicate downloads and builds.
8+
9+
## What is cached
10+
11+
### HTTP responses
12+
13+
This cache functions like a web browser cache.
14+
15+
When making any HTTP request, pip will first check its local cache to determine
16+
if it has a suitable response stored for that request which has not expired. If
17+
it does then it returns that response and doesn't re-download the content.
18+
19+
If it has a response stored but it has expired, then it will attempt to make a
20+
conditional request to refresh the cache which will either return an empty
21+
response telling pip to simply use the cached item (and refresh the expiration
22+
timer) or it will return a whole new response which pip can then store in the
23+
cache.
24+
25+
While this cache attempts to minimize network activity, it does not prevent
26+
network access altogether. If you want a local install solution that
27+
circumvents accessing PyPI, see {ref}`Installing from local packages`.
28+
29+
### Locally built wheels
30+
31+
pip attempts to use wheels from its local wheel cache whenever possible.
32+
33+
This means that if there is a cached wheel for the same version of a specific
34+
package name, pip will use that wheel instead of rebuilding the project.
35+
36+
When no wheels are found for a source distribution, pip will attempt to build a
37+
wheel using the package's build system. If the build is successful, this wheel
38+
is added to the cache and used in subsequent installs for the same package
39+
version.
40+
41+
```{versionchanged} 20.0
42+
pip now caches wheels when building from an immutable Git reference
43+
(i.e. a commit hash).
44+
```
45+
46+
## Avoiding caching
47+
48+
pip tries to use its cache whenever possible, and it is designed do the right
49+
thing by default.
50+
51+
In some cases, pip's caching behaviour can be undesirable. As an example, if you
52+
have package with optional C extensions, that generates a pure Python wheel
53+
when the C extension can’t be built, pip will use that cached wheel even when
54+
you later invoke it from an environment that could have built those optional C
55+
extensions. This is because pip is seeing a cached wheel for that matches the
56+
package being built, and pip assumes that the result of building a package from
57+
a package index is deterministic.
58+
59+
The recommended approach for dealing with these situations is to directly
60+
install from a source distribution instead of letting pip auto-discover the
61+
package when it is trying to install. Installing directly from a source
62+
distribution will make pip build a wheel, regardless of whether there is a
63+
matching cached wheel. This usually means doing something like:
64+
65+
```{pip-cli}
66+
$ pip download sampleproject==1.0.0 --no-binary :all:
67+
$ pip install sampleproject-1.0.0.tar.gz
68+
```
69+
70+
It is also a good idea to remove the offending cached wheel using the
71+
{ref}`pip cache` command.
72+
73+
## Cache management
74+
75+
The {ref}`pip cache` command can be used to manage pip's cache.
76+
77+
The exact filesystem structure of pip's cache is considered to be an
78+
implementation detail and may change between any two versions of pip.
79+
80+
## Disabling caching
81+
82+
pip's caching behaviour is disabled by passing the ``--no-cache-dir`` option.
83+
84+
It is, however, recommended to **NOT** disable pip's caching. Doing so can
85+
significantly slow down pip (due to repeated operations and package builds)
86+
and result in significantly more network usage.

docs/html/topics/index.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ This section of the documentation is currently being fleshed out. See
1111
:maxdepth: 1
1212
1313
authentication
14+
caching
1415
```

0 commit comments

Comments
 (0)