56

I've Python 3 running in a linux server. I need to install some libraries (obviously) so I'm trying :

pip3 install numpy

Which, is resulting in the following error:

Collecting numpy
  Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f7542572828>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/numpy/
  Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f7542572eb8>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/numpy/
  Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f7542572be0>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/numpy/
  Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f7542572d30>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/numpy/
  Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f7542572860>: Failed to establish a new connection: [Errno -2] Name or service not known',)': /simple/numpy/
  Could not find a version that satisfies the requirement numpy (from versions: )
No matching distribution found for numpy

Questions:

  1. What could be the problem? Why is this error being raised?
  2. How can this be avoided in the future?

Feel free to ask for more details.

UPDATE: I tried ping google.com and got the error:

ping: google.com: Name or service not known

But when I tried ping 8.8.8.8, I got:

PING 8.8.8.8 (8.8.8.8) 56(84) bytes of data.
64 bytes from 8.8.8.8: icmp_seq=1 ttl=118 time=10.8 ms
64 bytes from 8.8.8.8: icmp_seq=2 ttl=118 time=10.6 ms
64 bytes from 8.8.8.8: icmp_seq=3 ttl=118 time=10.6 ms
64 bytes from 8.8.8.8: icmp_seq=4 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=5 ttl=118 time=10.6 ms
64 bytes from 8.8.8.8: icmp_seq=6 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=7 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=8 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=9 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=10 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=11 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=12 ttl=118 time=10.8 ms
64 bytes from 8.8.8.8: icmp_seq=13 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=14 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=15 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=16 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=17 ttl=118 time=10.6 ms
64 bytes from 8.8.8.8: icmp_seq=18 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=19 ttl=118 time=10.6 ms
64 bytes from 8.8.8.8: icmp_seq=20 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=21 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=22 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=23 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=24 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=25 ttl=118 time=10.7 ms
64 bytes from 8.8.8.8: icmp_seq=26 ttl=118 time=10.7 ms
^C
--- 8.8.8.8 ping statistics ---
26 packets transmitted, 26 received, 0% packet loss, time 25046ms
rtt min/avg/max/mdev = 10.655/10.731/10.827/0.073 ms

A problem with DNS maybe? What should I do?

0

15 Answers 15

75

All I had to do was add nameserver 8.8.8.8 to the /etc/resolv.conf file. 8.8.8.8 is Google's public free DNS server's IP. So, since a system must know the IP address of a domain name, we have to give it a way to do just that. A public DNS server provides this binding of a domain name to the IP address.

6
  • 11
    Had this exact same issue with WSL and Virtualenv so thank you! Jan 20, 2019 at 10:31
  • 1
    Neat solution!!! Nov 23, 2021 at 22:31
  • 2
    Also had to restart WSL :) Dec 2, 2021 at 18:45
  • This is magic! I have suffered for months with this. Thanks a lot Jul 1, 2022 at 10:04
  • 1
    1) I actually had to remove the nameserver lines. 2) Just exiting the WSL was not enough for me. I had to use a powershell and do wsl --shutdown to really quit it.
    – ste
    Sep 8, 2023 at 20:23
27

For Windows 10 User, Go to Settings -> Network and Internet -> Proxy

Disable Automatically detect settings

enter image description here

5
  • 5
    Classic windows! I had to restart the computer to make this work.
    – user3521099
    Apr 11, 2021 at 22:42
  • 3
    I just wanted to say that I love you.
    – Marci-man
    Nov 28, 2021 at 23:51
  • 1
    Thanks for the tip. It worked. I didn't have to restart after making this change.
    – sgowd
    Apr 5, 2022 at 22:12
  • This isn't worked for me. Any other suggestion where I might be wrong? Jul 24, 2023 at 11:33
  • Unfortunately I don't feel this is much of a solution, as on a company issued laptop proxy settings can be controlled via group policy thus meaning the user doesn't have the ability to just turn off various Windows settings
    – Chris
    Sep 29, 2023 at 10:02
18

If using proxy network and facing same issue then use

pip install --proxy="proxy:portno" numpy

Foe example:

C:\Users\Dell>pip install --proxy="10.50.225.80:3128" numpy

3
  • Thanks, such a life saver ! My Windows automatic proxy set up was already turned off. This finally triggered the installation.
    – DataBach
    Jan 28, 2022 at 15:50
  • Thank you for the solution, now it works. Previously I had tried to set the proxy globally, by modifying ~/.bashrc, as shown bellow. However it didn't work. export http_proxy="proxy_url:proxy_port" export HTTP_PROXY="proxy_url:proxy_port" export https_proxy="proxy_url:proxy_port" export HTTPS_PROXY="proxy_url:proxy_port" export ftp_proxy="proxy_url:proxy_port" export FTP_PROXY="proxy_url:proxy_port" export all_proxy="proxy_url:proxy_port" export ALL_PROXY="proxy_url:proxy_port"
    – vcmorini
    Feb 10, 2022 at 13:12
  • 1
    It worked on my company laptop! To find the proxy on Windows I guessed that the port number was 8080 (most of the internal proxies work on port 8080), then I opened cmd and run netstat -a -n and I searched for 8080 with CTRL+F and I looked for an address with state "ESTABLISHED". Then I run pip install --proxy="proxy_found_in_cmd:8080" numpy and it worked!
    – sound wave
    Jan 18 at 14:31
18

I just restarted the network services and then did docker restart. Commands used:

systemctl restart NetworkManager.service
sudo service docker restart
2
  • 1
    This solution worked for me. I was using Ubuntu 22.04 virtual machine in VMWare. Nov 1, 2022 at 14:26
  • This worked for me without restarting NetworkManager (so, I only restarted docker service). Maybe it was caused by turning my VPN off and back on again some time before running docker-compose up. Nov 9, 2022 at 1:32
13

if you are trying to build a docker image and see this error use --network=host at the end of your command.

docker build -t {name} . --network=host
1
  • this helped me ,i was running the same command but inside the WSL (Ubuntu), thank you @mohammad May 24, 2023 at 12:02
9

Check the proxy configuration inside proxy settings. Then use :

 pip install --proxy=http://yourproxyurl:port numpy(Python package name)
2
  • 4
    I found it better to set environment vars for proxy to avoid errors with setup.py for modules. export http_proxy=<your-proxy:port>; export https_proxy=<your-https-proxy:port>
    – Junaid
    Feb 3, 2021 at 1:46
  • This works but for some reason, proxy does not apply to downloading dependencies, only main package is downloaded. For dependencies, I get same error Feb 15 at 14:37
7

Not an answer to the original question.

Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<urllib3.connection.VerifiedHTTPSConnection object at 0x7f7542572828>: Failed to establish a new connection: [Errno -2] Name or service not known',)':

The same warning occurs to me when I was writing Dockerfile.
Obviously, it's the network problem.
However, the result of DNS and ping from host to pip source is fine.
Eventually, I deleted the Dockerfile from the start of the pip part to the end.
After I exec into the container, the network is unable to get the DNS server.

Remind by my coworker, there was a change about iptables.
When a change made to iptables, the server needs to restart the docker engine to make it work.
It could be but not limit to sudo service docker restart
However, hope this answer could save some others' time.

0
4

I have this problem when I use VPN. Not sure what's the reason for this, but switching it off solves the problem.

2
  • Restarting docker (see Romita's answer above) seems to fix this. Nov 9, 2022 at 1:33
  • I have the same issue. Did you find out why it would not allow connection on VPN? Are there security vulnerabilities that the network admin, domain admin, or vpn admin would not allow connection for pip or python installation?
    – ptn77
    Jan 19, 2023 at 17:54
0

You can close all your proxy programs and leave the Windows proxy section blank. I had this problem with Lantern and Psiphon.

0

I had this problem on Raspbian 10 after intalling network-manager following the instructions in this link:

https://www.giovanni.panozzo.it/guides/raspbian-stretch-network-manager-openvpn.html

After undoing following step, DNS works again!

sudo ln -sf /run/resolvconf/resolv.conf /etc/resolv.conf

(luckily I saved the original file!)

0

It's purely network issue. so you just check your network speed for the pip package installations or restart your network and check again with pip install {{package_name}}

0

I ran into this issue using docker build. What is your docker version? Upgrading to the latest version helped me solve the issue!

Docker version 23.0.3
0

If you are on window use this :

py -m pip install --proxy="http://username:passwd@proxy-ip:port" package_name
0

In my case i needed to add hosts 'pypi.python.org' and 'pypi.org' to firewall' whitelist

0

I know that some other people have pointed out the fact that you can run pip with a proxy, but I'd like to point out that you can do this "globally" too. I am developing in an Ubuntu 20 VM, and for me, running

export http_proxy=[proxy info]
export https_proxy=[proxy info]
pip install flask

worked.

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