python安装使用pip安装PyExecJS时遇到了SSL证书验证失败的问题,提示是“CERTIFICATE_VERIFY_FAILED“解决方法.

先看错误报告

​

(.venv) PS E:\PythonDeme\01 _条件> pip install PyExecJS
WARNING: Retrying (Retry(total=4, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)'))': /simple/pyexecjs/
WARNING: Retrying (Retry(total=3, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)'))': /simple/pyexecjs/
WARNING: Retrying (Retry(total=2, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)'))': /simple/pyexecjs/
WARNING: Retrying (Retry(total=1, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)'))': /simple/pyexecjs/
WARNING: Retrying (Retry(total=0, connect=None, read=None, redirect=None, status=None)) after connection broken by 'SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)'))': /simple/pyexecjs/
Could not fetch URL https://pypi.org/simple/pyexecjs/: There was a problem confirming the ssl certificate: HTTPSConnectionPool(host='pypi.org', port=443): Max retries exceeded with url: /simple/pyexecjs/ (Caused by SSLError(SSLCertVerificationError(1, '[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate (_ssl.c:1007)'))) - skipping
ERROR: Could not find a version that satisfies the requirement PyExecJS (from versions: none)
ERROR: No matching distribution found for PyExecJS

[点击并拖拽以移动]
​

分析一下原因:

 Python 的 SSL 模块无法验证 PyPI 服务器的证书链,可能由以下原因导致:

  • 本地 CA 证书库过期或不完整(常见于企业网络环境)
  • 网络代理或防火墙拦截 HTTPS 请求
  • PyPI 镜像源证书配置问题

解决方法:

用 --trusted-host 参数(推荐)

通过添加信任的主机参数绕过证书验证:

以下是安装指令

pip install PyExecJS --trusted-host pypi.org --trusted-host files.pythonhosted.org --trusted-host pypi.python.org

<think>我们正在处理一个关于SSL证书验证失败问题,具体错误是`[SSL:CERTIFICATE_VERIFY_FAILED]certificateverifyfailed:unabletogetlocalissuercertificate`。这个错误通常发生在Python使用urllib或requests等库进行HTTPS请求,系统无法验证服务器的SSL证书,因为缺少中间证书或根证书。根据引用[3]的建议,我们可以从以下几个方面解决:1.确认错误来源:首先确保错误是由于服务器证书链不完整或本地证书缺失引起的。2.更新证书配置:更新本地的证书存储,确保包含最新的根证书和中间证书。3.检查服务器配置:确保服务器提供完整的证书链(包括所有中间证书)。4.解决方案(仅开发环境):在确保安全的情况下,可以暂禁用SSL验证(不推荐生产环境)。具体步骤:###1.更新本地证书存储Python通常使用系统的证书存储或自带的证书包(如`certifi`)。我们可以尝试更新`certifi`包,因为它提供了CA证书包。```bashpipinstall--upgradecertifi```然后,我们可以设置环境变量`SSL_CERT_FILE`指向更新后的证书文件(如引用[5]所示)。例如,在Windows系统中:```cmdsetSSL_CERT_FILE=C:\path\to\python\Lib\site-packages\certifi\cacert.pem```在Linux或macOS中:```bashexportSSL_CERT_FILE=/path/to/certifi/cacert.pem```###2.安装系统证书在某些系统(如macOS)上,Python可能没有使用系统的证书存储。我们可以尝试安装`certifi`并链接到系统证书,或者使用以下命令安装证书:对于macOS,可以运行以下命令来安装Python提供的证书:```bashopen/Applications/Python\3.10/Install\Certificates.command```注意:路径中的版本号(3.10)需要根据实际安装Python版本进行调整。###3.手动添加证书如果上述方法无效,可能需要手动将缺失的证书添加到证书文件。首先,我们需要获取服务器的证书链。可以使用OpenSSL命令:```bashopenssls_client-showcerts-connectexample.com:443```将输出中的证书(包括中间证书和根证书)保存为PEM格式文件,然后将其添加到`certifi`的cacert.pem文件中。###4.在代码中忽略证书验证(仅用于测试)在开发环境中,如果只是临需要,可以在请求禁用SSL验证(注意:这会带来安全风险,不推荐在生产环境中使用)。使用`urllib`:```pythonimporturllib.requestimportsslcontext=ssl._create_unverified_context()response=urllib.request.urlopen('https://example.com',context=context)```使用`requests`:```pythonimportrequestsresponse=requests.get('https://example.com',verify=False)```###5.检查服务器证书链确保服务器配置了完整的证书链。可以使用在线工具(如SSLLabs的SSLServerTest)检查服务器配置。###总结按照上述步骤,通常可以解决`unabletogetlocalissuercertificate`的问题。首先尝试更新证书存储,如果问题仍然存在,再考虑手动添加证书或检查服务器配置。在开发环境中,临禁用验证可以作为快速解决方案,但务必注意安全。§§相关问题§§1.如何更新Python的根证书?2.如何在不同的操作系统中设置SSL证书环境变量?3.如何检查服务器的SSL证书链是否完整?4.Python中,除了禁用验证,还有哪些处理SSL证书错误的方法?</think>### SSL证书验证失败问题解决方案 您遇到的`[SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed: unable to get local issuer certificate`错误通常发生在Python进行HTTPS请求,系统无法验证服务器的SSL证书链。以下是系统性的解决方案: #### 1. **更新根证书包**(推荐首选方案) Python使用`certifi`包提供的根证书集进行验证。更新步骤: ```bash # 升级certifi包 pip install --upgrade certifi # 设置环境变量指向新证书 # Windows: set SSL_CERT_FILE=%PythonPath%\Lib\site-packages\certifi\cacert.pem # Linux/macOS: export SSL_CERT_FILE=$(python -m certifi) ``` #### 2. **安装系统证书**(macOS特殊处理) 在macOS上运行Python自带的安装脚本: ```bash # 根据Python版本调整路径 sudo open "/Applications/Python 3.10/Install Certificates.command" ``` #### 3. **手动配置证书路径** 在代码中指定证书路径: ```python import ssl import certifi context = ssl.create_default_context(cafile=certifi.where()) response = urllib.request.urlopen(url, context=context) ``` #### 4. **验证服务器证书链** 使用OpenSSL诊断问题: ```bash openssl s_client -showcerts -connect example.com:443 ``` 检查输出中是否包含`Verify return code: 0 (ok)` #### 5. **临解决方案**(仅限开发环境) ```python # 禁用证书验证(有安全风险!) import ssl context = ssl._create_unverified_context() response = urllib.request.urlopen(url, context=context) ``` #### 6. **系统级修复** - **Windows**:更新系统根证书 1. 运行`certmgr.msc` 2. 导入新证书到"受信任的根证书颁发机构" - **Linux**:更新CA证书包 ```bash sudo apt update && sudo apt install ca-certificates ``` #### 根本原因分析 此错误通常由以下原因引起: 1. 中间证书缺失(服务器配置问题) 2. 本地根证书过期或缺失 3. Python证书包未更新 4. 系统间/日期错误(验证证书有效期失败)[^3] > **重要提示**:生产环境应始终启用证书验证,禁用SSL验证仅可作为临开发解决方案[^3]。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值