157

Where is the Python pip cache folder? I had an error during installation and now reinstall packages using cache files. Where is that directory? I want to take a backup of them for installation in the future. Is it possible?

For example, I have this one

Using cached cssselect-0.9.1.tar.gz

I searched google for this directory but nothing I saw, is learning how to install from a folder, I want to find the default cache directory.

And another question: Will these cache files stay in that directory, or will they be removed soon?

7
  • 1
    It depends on the OS. Jan 3, 2016 at 15:29
  • windows @MartinKonecny Jan 3, 2016 at 15:29
  • 3
    I believe it is in ~\AppData\Local\pip\cache on Windows.
    – fredrik
    Jan 3, 2016 at 15:30
  • there are many files with unclear names like '0f4017d43a13156e41129019e85a69fcc2dd34e6904ed88395bb5451'... can i find exactly 'cssselect-0.9.1.tar.gz' file ??? @fredrik Jan 3, 2016 at 15:33
  • 3
    A cache is not always human-readable, as in this case.
    – fredrik
    Jan 3, 2016 at 15:35

5 Answers 5

166

The default location for the cache directory depends on the Operating System:

Unix

~/.cache/pip and it respects the XDG_CACHE_HOME directory.

macOS

~/Library/Caches/pip

Windows

<CSIDL_LOCAL_APPDATA>\pip\Cache

Wheel Cache

pip will read from the subdirectory wheels within the pip cache directory and use any packages found there. [snip]

https://pip.pypa.io/en/latest/reference/pip_install/#caching

The location of the cache directory can be changed via the command line option --cache-dir.

4
  • 9
    <CSIDL_LOCAL_APPDATA> == %LOCALAPPDATA%
    – Winand
    May 29, 2019 at 10:35
  • 2
    %LOCALAPPDATA%\pip\Cache
    – Alex78191
    Nov 24, 2019 at 18:32
  • 1
    $XDG_CACHE_HOME is empty. find ~/.cache/pip | grep -i tensor shows tensorflow_determinism and silence_tensorflow wheels, but not tensorflow-gpu. Yet pip install tensorflow-gpu says Using cached https://.../tensorflow_gpu-2.0.0-cp37-cp37m-manylinux2010_x86_64.whl. Where else should I look for this file?
    – bers
    Nov 25, 2019 at 7:34
  • (I also downloaded tensorflow_gpu-2.0.0-cp37-cp37m-manylinux2010_x86_64.whl and checked for duplicates using fdupes, but could not find any...)
    – bers
    Nov 25, 2019 at 8:38
135

It depends on the operating system.

With pip 20.1 or later, you can find it with:

pip cache dir

For example with macOS:

$ pip cache dir
/Users/hugo/Library/Caches/pip

Docs:

4
  • Yeah ... It's an old question ( about 4 years :D ) Apr 30, 2020 at 10:03
  • 2
    Old but still relevant! github.com/hugovk/pypistats/pull/105/commits/…
    – Hugo
    May 1, 2020 at 12:26
  • 3
    See also the PIP_CACHE_DIR environment variable to set this directory.
    – Alex Povel
    Nov 10, 2020 at 15:51
  • $ pip install --no-deps --force black Downloading black-22.3.0-cp39-cp39-macosx_10_9_x86_64.whl (1.3 MB) Successfully installed black-22.3.0 $ pip install --no-deps --force black Using cached black-22.3.0-cp39-cp39-macosx_10_9_x86_64.whl (1.3 MB) Successfully installed black-22.3.0 $ pip cache list Nothing cached.
    – mrclary
    Apr 5, 2022 at 3:37
29

Pythonic and cross-platform way:

import pip
from distutils.version import LooseVersion

if LooseVersion(pip.__version__) < LooseVersion('10'):
    # older pip version
    from pip.utils.appdirs import user_cache_dir
else:
    # newer pip version
    from pip._internal.utils.appdirs import user_cache_dir

print(user_cache_dir('pip'))
print(user_cache_dir('wheel'))

Under the hood, it normalizes paths, manages different locations for exotic and ordinary operating systems and platforms, performs Windows registry lookup.

It may worth mentioning, if you have different Python versions installed, 2.x'es and 3.x'es, they all do share the same cache location.

3
  • 5
    ModuleNotFoundError: No module named 'pip.utils' , pip 10.0.1 on Ubuntu. Jun 1, 2018 at 2:50
  • 1
    If you wanted pip's own cache directory you could also just use from pip._internal.locations import USER_CACHE_DIR or python -c "from pip._internal.locations import USER_CACHE_DIR; print(USER_CACHE_DIR)" if you were grabbing things in a script, etc. Aug 15, 2019 at 11:35
  • 2
    Please do not go into _internal and fetch values. pip's internals are not meant to be used like a library and subject to change. pip cache dir on pip 20.1 and above is the best way to get this value.
    – pradyunsg
    May 13, 2020 at 19:07
10

You can backup the associated wheel rather than attempting to perform a backup of the cache folder.

Download the wheel for csselect of version 0.9.1 into /tmp/wheelhouse:

pip wheel --wheel-dir=/tmp/wheelhouse cssselect==0.9.1

Install the downloaded wheel:

pip install /tmp/wheelhouse/cssselect-0.9.1-py2-none-any.whl
2
  • How stable/predictable is the cssselect-0.9.1-py2-none-any.whl file name?
    – Blaise
    Sep 27, 2017 at 8:15
  • 1
    Depends on what the maintainers are doing in their setup.py when packaging.
    – fredrik
    Sep 28, 2017 at 5:53
1

Note that what pip caches is not necessarily human readable, and doing pip cache list does not necessarily list all the files that have been cached in some fashion. In addition to the .whl files that appear when you ask pip to list the cache, there is an http directory where network caching is done as well. On linux, it is in ~/.cache/pip/http. Files there do not appear to be listed when you do pip cache list

If you ask pip to install comm==0.1.3, it will look in the regular cache and when the network request is made, it will look in the network cache under a hashed key. The function _FileCacheMixin.encode() in the file pip/_internal/_vendor/cachecontrol/caches/file_cache.py (in pip version 23.0.1) turns the URL into a key. On my machine, the key is '8856a20c..[etc]' and there is a file ~/.cache/pip/http/8/8/5/6/a/8856a2..[etc] that is the whl file.

Upon attempting to install comm==0.1.3, pip will report:

Collecting comm==0.1.3
  Using cached comm-0.1.3-py3-none-any.whl (6.6 kB)

But pip cache list | grep comm comes up with nothing. This is a known issue. See: https://github.com/pypa/pip/issues/10460

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.