在Linux測試服務器上使用pip3安裝組件時,遇到下面錯誤:
#pip3 install cryptography
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', timeout('timed out'))': /simple/cryptography/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'ProxyError('Cannot connect to proxy.', timeout('timed out'))': /simple/cryptography/
....................................
ERROR: Could not find a version that satisfies the requirement cryptography (from versions: none)
ERROR: No matching distribution found for cryptography
查了一下相關資料弄清楚了這個錯誤出現的原因:一般出現這個錯誤跟本地網絡狀況或配置有關。一般而言,你可能默認使用了國外的pypi源。由於網絡狀況以及特殊國情,這種連接很容易出現超時,所以出現上面錯誤。
查看pypi源
#pip3 config list
解決方案
解決這個問題的方案很簡單,就是采用國內的鏡像源,一般常用的國內鏡像源有下面這些:
清華大學
https://pypi.tuna.tsinghua.edu.cn/simple/
http://pypi.tuna.tsinghua.edu.cn/simple/
阿里雲
https://mirrors.aliyun.com/pypi/simple/
http://mirrors.aliyun.com/pypi/simple/
中國科技大學
https://pypi.mirrors.ustc.edu.cn/simple/
http://pypi.mirrors.ustc.edu.cn/simple/
豆瓣(douban)
https://pypi.douban.com/simple/
http://pypi.douban.com/simple/
中國科學技術大學
https://pypi.mirrors.ustc.edu.cn/simple/
http://pypi.mirrors.ustc.edu.cn/simple/
測試驗證如下:
[root@KerryDB ~]# pip3 install cryptography -i http://mirrors.aliyun.com/pypi/simple/
Looking in indexes: http://mirrors.aliyun.com/pypi/simple/
WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.
ERROR: Could not find a version that satisfies the requirement cryptography (from versions: none)
ERROR: No matching distribution found for cryptography
WARNING: You are using pip version 20.2.3; however, version 21.1.1 is available.
You should consider upgrading via the '/usr/local/python3.8/bin/python3.8 -m pip install --upgrade pip' command.
如上所示,出現錯誤提示,這個是因為最新的pip要求源必須是https的,不然會報錯:
WARNING: The repository located at mirrors.aliyun.com is not a trusted or secure host and is being ignored. If this repository is available via HTTPS we recommend you use HTTPS instead, otherwise you may silence this warning and allow it anyway with '--trusted-host mirrors.aliyun.com'.
解決這個問題有兩個方法:
1:使用https源
2:pip install cryptography -i http://mirrors.aliyun.com/simple/ --trusted-host=mirrors.aliyun.com
如下測試驗證所示:
[root@KerryDB ~]# pip3 install cryptography -i https://mirrors.aliyun.com/pypi/simple/
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting cryptography
Downloading https://mirrors.aliyun.com/pypi/packages/b2/26/7af637e6a7e87258b963f1731c5982fb31cd507f0d90d91836e446955d02/cryptography-3.4.7-cp36-abi3-manylinux2014_x86_64.whl (3.2 MB)
|████████████████████████████████| 3.2 MB 288 kB/s
Collecting cffi>=1.12
Downloading https://mirrors.aliyun.com/pypi/packages/5c/0f/e07df370fac0e99e938edc62c8a15e54b9d75605e11838fa0ef300118e1d/cffi-1.14.5-cp38-cp38-manylinux1_x86_64.whl (411 kB)
|████████████████████████████████| 411 kB 3.5 MB/s
Collecting pycparser
Downloading https://mirrors.aliyun.com/pypi/packages/ae/e7/d9c3a176ca4b02024debf82342dab36efadfc5776f9c8db077e8f6e71821/pycparser-2.20-py2.py3-none-any.whl (112 kB)
|████████████████████████████████| 112 kB 1.2 MB/s
Installing collected packages: pycparser, cffi, cryptography
Successfully installed cffi-1.14.5 cryptography-3.4.7 pycparser-2.20
WARNING: You are using pip version 20.2.3; however, version 21.1.1 is available.
You should consider upgrading via the '/usr/local/python3.8/bin/python3.8 -m pip install --upgrade pip' command.
其實,最好是修改pip.conf設置,將國內某一個pypi源設置為默認源,這樣就不用每次使用pip3安裝包時要指定pypi源,如下所示:
[root@KerryDB ~]# pip3 config set global.index-url https://mirrors.aliyun.com/pypi/simple/
Writing to /root/.config/pip/pip.conf
[root@KerryDB ~]# pip3 config list
global.index-url='https://mirrors.aliyun.com/pypi/simple/'

