【Python】解决 pip 开了网络代理之后无法安装包的问题

问题描述

开了网络代理之后,python 的 pip 就无法安装包了,报如下错误:

$ pip install netsm
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1123)'))': /simple/netsm/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1123)'))': /simple/netsm/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1123)'))': /simple/netsm/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1123)'))': /simple/netsm/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1123)'))': /simple/netsm/
Could not fetch URL https://pypi.tuna.tsinghua.edu.cn/simple/netsm/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.tuna.tsinghua.edu.cn', port=443): Max retries exceeded with url: /simple/netsm/ (Caused by SSLError(SSLEOFError(8, 'EOF occurred in violation of protocol (_ssl.c:1123)'))) - skipping
ERROR: Could not find a version that satisfies the requirement netsm (from versions: none)
ERROR: No matching distribution found for netsm

原因分析

想必很多同学都和我一样,配置了 pip 源为国内的清华源或阿里源,这样下载包的速度会快,以清华源为例,配置方法如下:

pip config set global.index-url https://pypi.tuna.tsinghua.edu.cn/simple

然而,配置清华源之后,pip 安装的时候就是从清华的镜像仓库来抓取安装包,但是清华的网络会对一些境外的网络代理做屏蔽或过滤,因此导致开了代理之后,pip 就无法安装包了。

解决方案

临时方案

临时方案就很简单了,直接把代理关了就行了。但你要安装很多包的话,就很麻烦,得反复的开关代理,推荐下面的永久解决方案。

永久方案

既然 pip 镜像源(无论还是清华,亦或是阿里)对代理服务器进行了屏蔽,那么我们访问镜像源的时候就不要走代理网络了。再系统的代理设置里面,直接将镜像源的域名做隔断处理:

【Python】解决 pip 开了网络代理之后无法安装包的问题_第1张图片
即在代理服务器设置里面,请勿对以下条目开头的地址使用代理服务器,以清华源为例,就是添加 pypi.tuna.tsinghua.edu.cn,注意前面的分号 ;

你可能感兴趣的:(python,python,pip,网络)