今天,我在学习及实践使用 Python
虚拟环境时,下载相应库文件,直接使用 pip
下载,结果因下载速度过于实在太慢导致始终无法下载成功,一直会报异常。真是让人崩溃。
(myenv) [root@localhost pythonproject]# cat requirements.txt
ninja==1.11.1.1
ovs==2.17.1.post1
PyYAML==6.0
sortedcontainers==2.4.0
(myenv) [root@localhost pythonproject]# pip install -r requirements.txt
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ReadTimeoutError("HTTPSConnectionPool(host='pypi.org', port=443): Read timed out. (read timeout=15)")': /simple/ninja/
Collecting ninja==1.11.1.1 (from -r requirements.txt (line 1))
Using cached ninja-1.11.1.1-py2.py3-none-manylinux1_x86_64.manylinux_2_5_x86_64.whl.metadata (5.3 kB)
Collecting ovs==2.17.1.post1 (from -r requirements.txt (line 2))
Downloading ovs-2.17.1.post1-py3-none-any.whl.metadata (773 bytes)
Collecting PyYAML==6.0 (from -r requirements.txt (line 3))
Downloading PyYAML-6.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl.metadata (2.0 kB)
Collecting sortedcontainers==2.4.0 (from -r requirements.txt (line 4))
Downloading sortedcontainers-2.4.0-py2.py3-none-any.whl.metadata (10 kB)
Downloading ninja-1.11.1.1-py2.py3-none-manylinux1_x86_64.manylinux_2_5_x86_64.whl (307 kB)
━━━━╺━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 30.7/307.2 kB 2.1 kB/s eta 0:02:10
ERROR: Exception:
Traceback (most recent call last):
...
File "/home/xxx/pythonproject/myenv/lib/python3.8/site-packages/pip/_vendor/urllib3/response.py", line 443, in _error_catcher
raise ReadTimeoutError(self._pool, None, "Read timed out.")
pip._vendor.urllib3.exceptions.ReadTimeoutError: HTTPSConnectionPool(host='files.pythonhosted.org', port=443): Read timed out.
通过几次在不同机器上 pip
的使用,均发生上述的问题,让我对于默认的 pip
源的速度实在无法忍受,于是便搜集了一些国内的 pip
源,如下:
阿里云 http://mirrors.aliyun.com/pypi/simple/
中国科技大学 https://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban) http://pypi.douban.com/simple/
清华大学 https://pypi.tuna.tsinghua.edu.cn/simple/
中国科学技术大学 http://pypi.mirrors.ustc.edu.cn/simple/
使用方法很简单,直接 -i
加 url
即可!如下:
pip install scapy -i http://mirrors.aliyun.com/pypi/simple/
如果有如下报错:
请使用命令:
pip install scapy -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
如果想配置成默认的源,需要创建或修改配置文件(一般都是创建),方法如下:
Linux
的文件在 ~/.pip/pip.conf
Windows
在 %HOMEPATH%\pip\pip.ini
修改内容为:
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host= mirrors.aliyun.com
这样在使用 pip
来安装时,会默认调用该镜像。
让我们通过实际演示案例说明 pip
使用国内源的两种方式。
方式一:直接使用 pip install -i
安装
(myenv) [root@localhost pythonproject]# pip install -r requirements.txt -i http://mirrors.aliyun.com/pypi/simple/ --trusted-host mirrors.aliyun.com
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
Collecting ninja==1.11.1.1 (from -r requirements.txt (line 1))
Downloading http://mirrors.aliyun.com/pypi/packages/6d/92/8d7aebd4430ab5ff65df2bfee6d5745f95c004284db2d8ca76dcbfd9de47/ninja-1.11.1.1-py2.py3-none-manylinux1_x86_64.manylinux_2_5_x86_64.whl (307 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 307.2/307.2 kB 5.8 MB/s eta 0:00:00
Collecting ovs==2.17.1.post1 (from -r requirements.txt (line 2))
Downloading http://mirrors.aliyun.com/pypi/packages/f2/5a/3707617b3d8fe2467ca6dba793dccb0105260b774744406d69294383b62e/ovs-2.17.1.post1-py3-none-any.whl (123 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 123.1/123.1 kB 9.7 MB/s eta 0:00:00
Collecting PyYAML==6.0 (from -r requirements.txt (line 3))
Downloading http://mirrors.aliyun.com/pypi/packages/d7/42/7ad4b6d67a16229496d4f6e74201bdbebcf4bc1e87d5a70c9297d4961bd2/PyYAML-6.0-cp38-cp38-manylinux_2_5_x86_64.manylinux1_x86_64.manylinux_2_12_x86_64.manylinux2010_x86_64.whl (701 kB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 701.2/701.2 kB 6.8 MB/s eta 0:00:00
Collecting sortedcontainers==2.4.0 (from -r requirements.txt (line 4))
Downloading http://mirrors.aliyun.com/pypi/packages/32/46/9cb0e58b2deb7f82b84065f37f3bffeb12413f947f9388e4cac22c4621ce/sortedcontainers-2.4.0-py2.py3-none-any.whl (29 kB)
Installing collected packages: sortedcontainers, ninja, PyYAML, ovs
Successfully installed PyYAML-6.0 ninja-1.11.1.1 ovs-2.17.1.post1 sortedcontainers-2.4.0
(myenv) [root@localhost pythonproject]# pip list
Package Version
---------------- ------------
ninja 1.11.1.1
ovs 2.17.1.post1
pip 24.0
PyYAML 6.0
setuptools 47.1.0
sortedcontainers 2.4.0
通过上述演示例子可以看出,使用 pip
国内源直接即可根据指定软件的版本成功安装相应包。
方式二:配置默认 pip
源
(myenv) [root@localhost pythonproject]# cat ~/.pip/pip.conf
[global]
index-url = http://mirrors.aliyun.com/pypi/simple/
[install]
trusted-host= mirrors.aliyun.com
(myenv) [root@localhost pythonproject]# pip install ninja
Collecting ninja
Downloading http://mirrors.aliyun.com/pypi/packages/6d/92/8d7aebd4430ab5ff65df2bfee6d5745f95c004284db2d8ca76dcbfd9de47/ninja-1.11.1.1-py2.py3-none-manylinux1_x86_64.manylinux_2_5_x86_64.whl (307kB)
100% |████████████████████████████████| 307kB 2.0MB/s
Installing collected packages: ninja
Successfully installed ninja-1.11.1.1
You are using pip version 9.0.3, however version 24.0 is available.
You should consider upgrading via the 'pip install --upgrade pip' command.
通过上述案例可以看出,当以配置文件配置了默认的 pip
源时,直接就可以通过 pip
命令安装相应的软件包。
如果 pip
版本 >=10.0.0
,可以使用如下命令进行设置:
pip config set global.trusted-host mirrors.aliyun.com
pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/
通过命令行配置完成后,生成配置文件如下:
(myenv) [root@localhost pythonproject]# pip config set global.trusted-host mirrors.aliyun.com
Writing to /root/.config/pip/pip.conf
(myenv) [root@localhost pythonproject]# pip config set global.index-url http://mirrors.aliyun.com/pypi/simple/
Writing to /root/.config/pip/pip.conf
(myenv) [root@localhost pythonproject]# cat /root/.config/pip/pip.conf
[global]
trusted-host = mirrors.aliyun.com
index-url = http://mirrors.aliyun.com/pypi/simple/
否则,使用上面的操作进行源配置或者更新 pip
版本:
pip install -U pip
或
pip3 install -U pip