解决Python pip install SSL证书验证失败错误
本文详细解析Python中使用pip install时遇到的SSL: CERTIFICATE_VERIFY_FAILED错误。提供两种解决方案:一是通过配置pip.ini文件指定国内镜像源(如阿里云)以绕过证书验证;二是在CentOS7等Linux环境下,通过--trusted-host参数临时跳过SSL检查。适用于Windows和Linux系统,帮助开发者快速恢复包安装功能。
Windows下pip SSL证书验证失败错误现象
当执行pip install命令时,可能会遇到如下异常报错信息:
C:\Users\ccwant>pip install requests Collecting requests Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)'),)': /simple/requests/ Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)'),)': /simple/requests/ Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)'),)': /simple/requests/ Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)'),)': /simple/requests/ Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)'),)': /simple/requests/ Could not fetch URL https://pypi.org/simple/requests/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/requests/ (Caused by SSLError(SSLError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:645)'),)) - skipping Could not find a version that satisfies the requirement requests (from versions: ) No matching distribution found for requests
通过配置pip.ini修改国内镜像源
针对上述问题,可以采取以下处理措施:
首先,需要访问系统用户配置目录,并在该目录下新建一个名为pip的文件夹。
具体路径为:C:\Users\用户\AppData\Roaming
进入刚才创建的pip文件夹后,新建一个名为pip.ini的配置文件,并在其中写入以下内容:
[global] index-url = http://mirrors.aliyun.com/pypi/simple/ [install] trusted-host = mirrors.aliyun.com
上述配置的主要目的是将pip的默认安装源更改为阿里云镜像。完成此设置后,再次使用pip install命令安装包时,即可避免因SSL证书验证失败而导致的错误。
Linux环境下跳过SSL验证的临时方案
补充知识:在Python3环境中使用pip install时,若提示"SSL: CERTIFICATE_VERIFY_FAILED",可参考以下Linux场景的处理方式。
在CentOS7系统中,若通过官方yum源安装了Python3和pip(若yum搜索不到python3,请先安装epel-release),可能会遇到相关SSL问题。
[root@client0 yum.repos.d]# python3 --version Python 3.6.8 [root@client0 yum.repos.d]# pip3 --version pip 8.1.2 from /usr/lib/python3.6/site-packages (python 3.6)
在执行pip安装包时,系统可能会提示当前pip版本过低,并建议使用pip install --upgrade pip命令进行升级。
然而,执行该升级命令时,往往会输出一系列错误提示,其中包含关键信息"SSL: CERTIFICATE_VERIFY_FAILED"。
此时,推荐的解决方法是跳过pip的SSL证书验证步骤。
可以通过添加特定参数来执行安装命令,具体命令如下:
pip3 install --trusted-host pypi.org --trusted-host files.pythonhosted.org <package_name>
执行上述命令即可完成安装。
以上关于Python pip install SSL异常处理的解决方案仅供参考,希望能帮助读者解决问题,欢迎继续支持相关技术分享。
本站声明
本文内容仅供参考,不代表本站观点。转载或引用请注明出处。如发现内容问题,请通过联系我们页面反馈。













