下面来分享“PIP安装python包出现超时问题的解决”的完整攻略:
在使用pip安装Python包时,常常会出现超时(Timeout)的错误提示,例如:
Collecting pandas
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPConnection object at 0x7fe39251d340>: Failed to establish a new connection: [Errno -2] Name or service not known')': /simple/pandas/
这种错误提示通常表示pip所链接的PyPI服务器崩溃或者网络环境较差,导致连接超时。
为了解决这个问题,可以按照以下步骤:
pip下载Python包的默认源是从PyPI服务器下载,因此当PyPI服务器不可用时,pip就会超时。
手动配置pip源可以使pip从其他可用的Python包源下载包,例如清华大学PyPi镜像。因此,我们可以打开终端,运行以下命令:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
这里我们以清华大学PyPi镜像为例。这条命令会将pip的默认源更改为清华大学的源,如果您未遇到此问题,可以跳过这一步。
另外一种方法是增加pip超时参数,使pip在下载超时时自动重试。我们打开终端,运行以下命令:
pip install --default-timeout=1000 pandas
这里我们以安装pandas为例,--default-timeout=1000 的意思是设置pip超时时间为1000秒。您可以自行调整这个值,比如将其调整为1500秒或更大。
接下来我将通过两个示例来说明如何使用以上两种方法:
当我在执行 pip install flask
命令时,首次尝试下载 flask 包时会提示超时错误,如下所示:
Collecting flask
Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPConnection object at 0x10d8fe5b0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /simple/flask/
Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPConnection object at 0x10d90f910>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /simple/flask/
Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPConnection object at 0x10d90f1f0>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /simple/flask/
Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPConnection object at 0x10d90f490>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /simple/flask/
Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'NewConnectionError('<pip._vendor.urllib3.connection.VerifiedHTTPConnection object at 0x10d90fb20>: Failed to establish a new connection: [Errno 8] nodename nor servname provided, or not known')': /simple/flask/
Could not find a version that satisfies the requirement flask (from versions: )
No matching distribution found for flask
为解决该问题,我执行以下命令更换pip源:
pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple
然后再次执行 pip install flask
,这次下载 flask 包就成功了。
以安装 pandas 包为例,我们使用 pip install --default-timeout=1000 pandas
命令来设置超时时间为1000秒,如下所示:
pip install --default-timeout=1000 pandas
这条命令会安装 pandas 包时自动重试,避免超时问题。如果您需要设置超时时间为其他值,只需将命令中的 1000 替换为需要的超时时间即可。
以上就是解决“pip安装python包出现超时问题”的完整攻略,希望对您有所帮助。
本文链接:http://task.lmcjl.com/news/13669.html