安装库时,Python pip引发NewConnectionError

Moo*_*ter 3 install pip server

我已经在Linux服务器上运行了Python 3。我需要安装一些库(显然),所以我在尝试:

pip3 install numpy
Run Code Online (Sandbox Code Playgroud)

这会导致以下错误:

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
Run Code Online (Sandbox Code Playgroud)

问题:

  1. 可能是什么问题呢?为什么会引发此错误?
  2. 将来如何避免这种情况?

随意询问更多细节。

更新: 我尝试ping google.com并得到了错误:

ping: google.com: Name or service not known
Run Code Online (Sandbox Code Playgroud)

但是当我尝试时ping 8.8.8.8,我得到了:

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
Run Code Online (Sandbox Code Playgroud)

DNS可能有问题吗?我该怎么办?

Rom*_*kur 18

我刚刚重新启动了网络服务,然后重新启动了docker。使用的命令:

systemctl restart NetworkManager.service
sudo service docker restart
Run Code Online (Sandbox Code Playgroud)


小智 12

如果您尝试构建 docker 映像并--network=host在命令末尾看到此错误,请使用。

docker build -t {name} . --network=host
Run Code Online (Sandbox Code Playgroud)


Moo*_*ter 8

我要做的就是添加nameserver 8.8.8.8到/etc/resolv.conf文件中。8.8.8.8是Google的公共免费DNS服务器的IP。因此,由于系统必须知道域名的IP地址,因此我们必须提供一种做到这一点的方法。公共DNS服务器将域名绑定到IP地址。

  • WSL 和 Virtualenv 遇到了完全相同的问题,谢谢! (2认同)
  • 还必须重新启动 WSL :) (2认同)

小智 8

检查代理设置中的代理配置。然后使用:

 pip install --proxy=http://yourproxyurl:port numpy(Python package name)
Run Code Online (Sandbox Code Playgroud)

  • 我发现最好为代理设置环境变量,以避免模块的 setup.py 出现错误。`export http_proxy=&lt;您的代理:端口&gt;; 导出 https_proxy=&lt;您的 https-proxy:端口&gt;` (4认同)

Shi*_*ang 7

不是原始问题的答案。

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',)':
Run Code Online (Sandbox Code Playgroud)

当我编写 Dockerfile 时,我也遇到了同样的警告。
显然是网络问题。
但是,DNS 和从主机到 pip 源的 ping 结果都很好。最终,我从该部分的开头到结尾
删除了 Dockerfile 。 进入容器后,网络无法获取DNS服务器。pip
exec

同事提醒,有变化iptables
当对 进行更改时iptables,服务器需要重新启动 docker 引擎才能使其工作。
可能但不限于sudo service docker restart
但是,希望这个答案可以节省其他人的时间。


小智 6

对于 Windows 10 用户,请转到 Settings -> Network and Internet -> Proxy

禁用自动检测设置

在此处输入图片说明

  • 经典窗户!我必须重新启动计算机才能使其工作。 (5认同)
  • 我只是想说我爱你。 (3认同)

小智 5

如果使用代理网络并面临同样的问题,则使用

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

敌例:

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