|
| 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. |
0 commit comments