8

Trying to install packages for python in Windows 10 machine and python 3.7.2. I'm using the following command:

pip install numpy

And it hangs forever. I tried to get more information using the following:

pip -vvv install numpy

and here's the result:

Collecting numpy
  1 location(s) to search for versions of numpy:
  * https://pypi.org/simple/numpy/
  Getting page https://pypi.org/simple/numpy/
  Looking up "https://pypi.org/simple/numpy/" in the cache
  Request header has "max_age" as 0, cache bypassed
  Starting new HTTPS connection (1): pypi.org:443
  https://pypi.org:443 "GET /simple/numpy/ HTTP/1.1" 304 0

I tried to research it but can't find anything. I can't believe only this package would go through HTTPS and that's why it's failing?

Moseleyi
  • 2,585
  • 1
  • 24
  • 46
  • Can you try: a)`pip download numpy`, b) `pip install numpy*.whl` ? – Alex Yu Feb 14 '19 at 10:04
  • Try `pip3 install numpy` – Heyran.rs Feb 14 '19 at 10:06
  • @AlexYu `pip download numpy` also hangs at the same point – Moseleyi Feb 14 '19 at 10:59
  • Did you tried `--no-cache-dir` as @GuilleC suggested? Are you under firewall? What will became if you: a) install [pipenv](https://github.com/pypa/pipenv) b) `pipenv install numpy` ? – Alex Yu Feb 14 '19 at 11:23
  • Yes the `--no-cache-dir` worked if I install the package on its own but not if I add it to the command like `pip install -r req.txt` and numpy is just a dependency of one of the packages to be installed.. – Moseleyi Feb 14 '19 at 12:19

4 Answers4

8

I had the same issue with Django.

The diff in the output of both commands is the following:

pip install Django -vvv
...
Looking up "https://pypi.org/simple/django/" in the cache
Request header has "max_age" as 0, cache bypassed
https://pypi.org:443 "GET /simple/django/ HTTP/1.1" 304 0
<hangs here>

$ pip install Django --no-cache-dir -vvv
...
https://pypi.org:443 "GET /simple/django/ HTTP/1.1" 200 27460
<continues and successfully installs>

Using --no-cache-dir just bypasses the problem.

The solution came when I manually deleted the contents of the cache directory.

rm -Rf ~/.cache/pip/* allowed pip install Django to work as expected, and the cache started rebuilding itself again.

From the docs you can find the path where the cache lives, based on your os:

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

raratiru
  • 8,748
  • 4
  • 73
  • 113
  • That's true, it just bypasses the problem. I still haven't found a long-term solution – Moseleyi Apr 12 '19 at 07:39
  • @Moseleyi In my case, when I deleted the cache `rm -Rf ~/.cache/pip/*`, `pip install Django` started working without `--no-cache-dir` and the cache started to rebuild again. – raratiru Apr 12 '19 at 12:01
  • hm but how can this be automated? We have a text file called `requirements` with list of packages to install. I'm not sure I can incpororate this in such script? – Moseleyi Apr 12 '19 at 13:10
  • @Moseleyi Oh, no, I did it once manually. When the contents of the cache were removed along with the content that caused pip to freeze hanging unable to do anything, `pip install -r requirements.txt` is "forever" working as expected without modifications, without `--no-cache-dir`. – raratiru Apr 12 '19 at 13:28
  • @Moseleyi There are also posts [1](https://github.com/pypa/pip/issues/4830) [2](https://github.com/pypa/pip/issues/5110) in pip issues and [SO](https://stackoverflow.com/a/39510975/2996101) implying that is safe to delete the cache. – raratiru Apr 13 '19 at 08:02
  • It worked, I deleted the folder and everything went smoothly. Needed to install some C++ Compiler and that's it. Then added everything to the Path and finally it worked! :D – Moseleyi Apr 16 '19 at 13:41
2

Yo can try adding:

--no-cache-dir

By default, when making any HTTP request pip will first check its local cache to determine if it has a suitable response stored for that request which has not expired. If the error comes in that part of the process, skipping that cache check should fix the problem.

Details in the official pip documentation.

Casti
  • 157
  • 1
  • 14
  • 1
    Can you elaborate why it would make a difference? – Moseleyi Feb 14 '19 at 10:04
  • 1
    Http 304 means "Not Modified". I did not look into source of pip but it's possible that package in cache prevents installation – Alex Yu Feb 14 '19 at 10:07
  • It worked but it wouldn't work with a file of packages to install. For example this: `pip install -r req.txt` had package `pandas` which has `numpy` as dependency. Adding `--no-cache-dir` didnt' work there and it still hung but it worked when I install numpy on its own – Moseleyi Feb 14 '19 at 10:56
2

As a workaround, you could manually download and install numpy

Go to here and choose the .whl file of the version you'd like installed: https://pypi.org/simple/numpy/

Once it's downloaded you can manually install the .whl:

pip install numpy-1.16.1-cp37-cp37m-win_amd64.whl

Alan Kavanagh
  • 9,425
  • 7
  • 41
  • 65
2

IPv6 somehow caused this problem for me. After disabling ipv6 networking on my device, the command went through fine!

Sush
  • 1,169
  • 1
  • 10
  • 26