pip uses incorrect cached package version, instead of the user-specified version
Asked Answered
D

19

687

I need to install psycopg2 v2.4.1 specifically. I accidentally did:

pip install psycopg2

Instead of:

pip install psycopg2==2.4.1

That installs 2.4.4 instead of the earlier version.

Now even after I pip uninstall psycopg2 and attempt to reinstall with the correct version, it appears that pip is re-using the cache it downloaded the first time.

How can I force pip to clear out its download cache and use the specific version I'm including in the command?

Diocletian answered 1/3, 2012 at 4:6 Comment(0)
D
712

If using pip 6.0 or newer, try adding the --no-cache-dir option (source).

If using pip older than pip 6.0, upgrade it with pip install -U pip.

Drooff answered 1/3, 2012 at 4:26 Comment(8)
I looked in ~/.pip but its an empty directory. Your tip on --ignore-installed did the trick!Diocletian
On OSX, I had to delete the pip related directories in $TMPDIRPenitentiary
If using virtualenv you might have to delete envs/{yourenvsname}/build/{packageinquestion} dir...Compete
In windows: I've seen pip caching directory is in ~\AppData\Local\pip\cacheMorbidity
@Dr Manhattan's answer locates the pip cache on the major OSs.Jeffersonjeffery
You have to do python -m pip install -U pip on Windows. (It can't upgrade due to locking the pip.exe file otherwise.)Doge
This answer is only partially correct. --no-cache-dir does not delete contents of cache folder.Jellaba
This answer was correct back in 2012 - the answer from @pradyunsg is much better practice now.Ethyl
M
639

Clear the cache directory where appropriate for your system

Linux and Unix

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

OS X

~/Library/Caches/pip

Windows

%LocalAppData%\pip\Cache

UPDATE

With pip 20.1 or later, you can find the full path for your operating system easily by typing this in the command line:

pip cache dir

Example output on my Ubuntu installation:

➜ pip3 cache dir
/home/tawanda/.cache/pip
Mainz answered 4/8, 2015 at 11:7 Comment(2)
The current most-up-to-date answer (posted by a pip maintainer) is https://mcmap.net/q/49603/-pip-uses-incorrect-cached-package-version-instead-of-the-user-specified-version. The best approach now is pip cache purge.Ritzy
pip cache purge only works on pip 20.1 according to the answer you link to. This answer seems to apply to all pip versions and all platforms, which would make it more generally useful but I don't know if that's the case.Dietetic
R
368

With pip 20.1 or later, you can do:

  • pip cache remove matplotlib: removes all wheel files related to matplotlib from pip's cache.
  • pip cache purge: to clear all wheel files from pip's cache.
  • pip cache dir: to get the location of the cache.

If you want to not use the pip cache for some reason (which is a bad idea, according the official docs), your options are:

  • pip install --no-cache-dir <package>: install a package without using the cache, for just this run.
  • pip config set global.no-cache-dir false: configure pip to not use the cache "globally" (in all commands).

Some history around this question (puts on pip maintainer hat):

The specific issue of "installing the wrong version due to caching" issue mentioned in the question was fixed in pip 1.4, back in 2013!)

Fix a number of issues related to cleaning up and not reusing build directories. (#413, #709, #634, #602, #939, #865, #948)

Since pip 6.0 (back in 2014!), pip install, pip download and pip wheel commands can be told to avoid using the cache with the --no-cache-dir option. (e.g. pip install --no-cache-dir <package>)

Back then, yes, passing --no-cache-dir was the only option to avoid this bug. So... it's a bit unfortunate that this is the top search result on "pip cache remove". :)

Since pip 10.0 (back in 2018!), a pip config command was added, which can be used to configure pip to always ignore the cache. This was always possible by manually editing the relevant files, but this surfaced that ability to the command line. Details on pip's configuration mechanisms is available here.

Since pip 20.1, pip has a pip cache command to manage the contents of pip's cache.

Ritzy answered 12/5, 2020 at 21:29 Comment(8)
I have pip 20.1.1 but when I try pip cache purge I get next error: ERROR: No matching packages but I installed a lot of different packages alreadyProdigious
@mikhail_sam try pip cache list if it doesn't show anything than you don't have anything cached.Mandamandaean
@RobertLugg looks like you are right! pip cache list returns Nothing cached.Prodigious
For me pip cache purge and pip cache list suggest cache is empty but du -hs ~/.cache/pip shows 242MB.Carlcarla
There is a typo in pip config set global.cache-dir false. It should be pip config set global.no-cache-dir false.Horizontal
I get an error ERROR: unknown command "cache" - maybe you meant "check"Antilles
@Antilles You're on a verson of pip older than 20.1.Ritzy
Good point, @Darren; I've edited the answer accordingly, and have added a note on how to re-enable the cache. (Hope I got it right; as noted in this answer, using false is counter-intuitive, but it seems to work; as of (at least) v22.0.3, true also works and seemingly has the same effect(!); to re-enable the cache, the setting must be unset (removed)).Erythrocyte
S
109

From documentation at https://pip.pypa.io/en/latest/reference/pip_install.html#caching:

Starting with v6.0, pip provides an on-by-default cache which functions 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.

Scleroderma answered 27/12, 2014 at 10:4 Comment(2)
This is the right answer...the link also shows where pip stashes the cache on Linux, Windows & OS X.Fournier
And to add, if you want to remove the "bad" object from your cache, have a look at the page to find the location of the cache file, and "find" the offending package. Linux is ~/.cache/pip, Mac is ~/Library/Caches/pip, etc. Interestingly, psycopg2 was also my problem package, but it was because the existing package was compiled for a different Postgresql library, which no longer existing on my server.Cristiecristin
M
95

pip can install a package ignoring the cache, like this

pip --no-cache-dir install scipy
Maure answered 8/4, 2015 at 11:39 Comment(1)
@dafeda's answer provides the same information 4 months agoModify
C
46

Since pip 20.1b1, which was released on 21 April 2020 and "added pip cache command for inspecting/managing pip’s wheel cache", it is possible to issue this command:

pip cache purge

The reference guide is here:
https://pip.pypa.io/en/stable/reference/pip_cache/
The corresponding pull request is here.

Chrysoberyl answered 28/4, 2020 at 20:51 Comment(3)
What about pip3? Using pip3 i got ERROR: unknown command "cache" - maybe you meant "check"Perpend
Then possibly you should upgrade your pip (or pip3, which I think should be the same thing since Python 2 was phased out): pip install --upgrade pip or pip3 install --upgrade pip.Tampere
Even after updating pip I get the error ERROR: unknown command "cache" - maybe you meant "check"Antilles
T
32

On Ubuntu, I had to delete /tmp/pip-build-root.

Tuberculate answered 7/5, 2013 at 19:42 Comment(2)
Actually /tmp/pip-build-%username_that_running_pip%Slurry
On Ubuntu 14 it was /tmp/pip_build_root/ (note underscores)Yesterday
L
16

If you like to set the --no-cache-dir option by default, you can put this into pip.conf:

[global]
no-cache-dir = false

Note 1: It's confusing, but to enable the no-cache-dir option you actually have to set it to false. Pretty silly if you ask me... but that's how it is. There is a github issue to fix this.

Note 2: The location of pip.conf depends on your OS. See the documentation for more info.

Lockyer answered 26/8, 2018 at 19:8 Comment(1)
Are they really debating for 3 years now about the justification to activate an option using False instead of True ? -REALLY- ?Orff
B
9

I just had a similar problem and found that the only way to get pip to upgrade the package was to delete the $PWD/build (%CD%\build on Windows) directory that might have been left over from a previously unfinished install or a previous version of pip (it now deletes the build directories after a successful install).

Braided answered 10/4, 2012 at 2:16 Comment(0)
F
8

On archlinux pip cache is located at ~/.cache/pip, I could solve my issue by removing the http folder inside it.

Floatable answered 27/4, 2015 at 23:13 Comment(0)
I
7

On my mac I had to remove the cache directory ~/Library/Caches/pip/

Interweave answered 6/7, 2015 at 6:9 Comment(0)
A
6

Simply

rm -d -r "$(pip cache dir)"
Amenable answered 7/2, 2021 at 6:23 Comment(0)
A
5
(pyvenv.d) jdoe$ pip --version       # pip version for this answer (or newer).
pip 21.1.1

(pyvenv.d) jdoe$ pip cache --help    # Review all options available to you.

(pyvenv.d) jdoe$ pip cache dir       # Cache-directory for pip(1).
/home/jdoe/.cache/pip

(pyvenv.d) jdoe$ pip cache purge     # Purge cache-directory (by example).
Files removed: 621                   # If cache-directory is already empty, the
                                     # output will be: "ERROR: No matching packages".
Alvarez answered 8/5, 2021 at 20:51 Comment(0)
T
4

On Windows 7, I had to delete %HOMEPATH%/pip.

Tuberculate answered 7/5, 2013 at 21:4 Comment(0)
C
3

If using virtualenv, look for the build directory under your environments root.

Compete answered 11/12, 2013 at 15:40 Comment(0)
R
3

I had to delete %TEMP%\pip-build On Windows 7

Reseat answered 12/8, 2014 at 8:59 Comment(1)
Thanks+1. I'm also using Windows 7 and found the folder under my %TEMP%\pip folder. The %TEMP% is defined in your environmental variables if anyone is unsure.Tannate
S
2

On Mac OS (Mavericks), I had to delete /tmp/pip-build/

Spireme answered 7/2, 2014 at 19:11 Comment(0)
A
0

A better way to do it is to delete the cache and rebuild it. In this way, if you install it again for other virtualenv, it will use the cache instead of building every time when you install it.

For example, when you install it, it will say it uses cached wheel,

Processing <some_prefix>/Library/Caches/pip/wheels/d0/c4/e4/e49fd07bca8dda00dd6b4bbc606aa05a25aacb00d45747a47a/horovod-0.19.3-cp37-cp37m-macosx_10_9_x86_64.wh

Just delete that one and restart your install.

Algeciras answered 28/5, 2020 at 17:41 Comment(0)
C
-2

(...) it appears that pip is re-using the cache (...)

I'm pretty sure that's not what's happening. Pip used to (wrongly) reuse build directory not cache. This was fixed in version 1.4 of pip which was released on 2013-07-23.

Carnet answered 15/2, 2016 at 11:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.