在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/'