Mac OSX python ssl.SSLError:[SSL:CERTIFICATE_VERIFY_FAILED] 证书验证失败(_ssl.c:749)
- 2025-02-21 08:50:00
- admin 原创
- 57
问题描述:
Python 中的许多操作都需要通过 https 访问。这包括pip install
命令,或仅使用http.client.HTTPSConnection
,或任何在内部使用这些内容的模块或应用程序。
如果 python 是从官方 python pkg 安装程序(从https://python.org下载)安装的,则它使用 openssl 的内部版本,并且不包含根证书。任何使用 SSL 连接的操作都会导致此错误:
ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
我如何安装根证书才能消除上述错误?
解决方案 1:
当你运行 python 安装程序时,它们会向你显示此信息。它也记录在 中/Applications/Python 3.6/ReadMe.rtf
,但很容易被忽略。
只需浏览Applications/Python 3.6
并双击Install Certificates.command
Python 错误跟踪器中有一个关于此问题的问题。http
://bugs.python.org/issue29480
更新:此问题在错误跟踪器中被标记为已解决,以下文本是最新评论的一部分:
... 对于 3.7.0b2,我尝试通过两种方式使事情变得更加明显。第一,安装程序包现在将尝试打开包含“Install Certifications.command”的 /Application/Python 3.7 文件夹的 Finder 窗口。第二,安装结束时不再只是出现通用的“安装完成”消息,而是出现定制消息,敦促用户单击“Install Certifications.command”图标。...
解决方案 2:
我使用以下命令解决了这个问题:
open /Applications/Python 3.7/Install Certificates.command
我的机器上有 Python 3.7。
解决方案 3:
解决所有 Python 版本问题的好方法,无需检查 macOS 上的版本
bash /Applications/Python*/Install Certificates.command
该命令等效于:
...
bash /Applications/Python 2.7/Install Certificates.command
bash /Applications/Python 3.6/Install Certificates.command
bash /Applications/Python 3.7/Install Certificates.command
...
它帮助了我,希望它也能帮助你
解决方案 4:
如果 pip 无法解决问题
pip3 install --upgrade certifi
如果找不到“ Install Certifications.command ” ,请尝试以下脚本
#!/usr/bin/env python3
# install_certifi.py
#
# sample script to install or update a set of default Root Certificates
# for the ssl module. Uses the certificates provided by the certifi package:
# https://pypi.python.org/pypi/certifi
import os
import os.path
import ssl
import stat
import subprocess
import sys
STAT_0o775 = ( stat.S_IRUSR | stat.S_IWUSR | stat.S_IXUSR
| stat.S_IRGRP | stat.S_IWGRP | stat.S_IXGRP
| stat.S_IROTH | stat.S_IXOTH )
def main():
openssl_dir, openssl_cafile = os.path.split(
ssl.get_default_verify_paths().openssl_cafile)
# +++> if already done <----
#print(" -- pip install --upgrade certifi")
#subprocess.check_call([sys.executable,
# "-E", "-s", "-m", "pip", "install", "--upgrade", "certifi"])
import certifi
# change working directory to the default SSL directory
os.chdir(openssl_dir)
relpath_to_certifi_cafile = os.path.relpath(certifi.where())
print(" -- removing any existing file or link")
try:
os.remove(openssl_cafile)
except FileNotFoundError:
pass
print(" -- creating symlink to certifi certificate bundle")
os.symlink(relpath_to_certifi_cafile, openssl_cafile)
print(" -- setting permissions")
os.chmod(openssl_cafile, STAT_0o775)
print(" -- update complete")
if __name__ == '__main__':
main()
解决方案 5:
如果您使用的是 MacOS,请转到应用程序 >> python3.8 >> 并双击 Install Certifications.command。这对我有用。
解决方案 6:
export SSL_CERT_FILE=$(python -m certifi)
效果很好。请确保您已事先安装了 certifi。
解决方案 7:
就我而言,所有解决方案都不适用于在 macOS Catalina 中安装了 python3 的系统,也不适用于通过 brew 安装的 python3。
如果有人遇到这种情况并希望快速解决,请使用https://www.python.org/downloads/
再次下载并安装 python3
安装结束时,安装程序会显示一条提示,要求您运行该Install Certificates.command
文件。
(在其他安装中,此文件不存在,并且文件源代码的解决方案也不起作用)
重新启动终端,然后输入where python3
,即可看到/Library/Frameworks/Python.framework/Versions/3.8/bin/python3
。使用此二进制文件,问题应该不会发生。
注意:可能可以使系统安装的 python3 工作,但是就我而言,这被证明非常困难,所以我选择了这种方式。
解决方案 8:
当我尝试使用 Keras 数据加载运行 Python 时,我遇到了同样的问题。对我来说错误是:
Exception: URL fetch failure on AWS_URL: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)
我通过升级证书解决了我的问题:
pip install --upgrade certifi
解决方案 9:
确认您不在虚拟环境中。我尝试了上述操作但没有成功,后来才意识到我的安装失败是因为我处于虚拟环境中
解决方案 10:
我这样做了,它在我的 Mac 上运行良好:Macintoch HD>应用程序>Python 文件>安装证书.command(双击它)
解决方案 11:
解决这个问题的一个便宜的方法就是使用 python3.5(如果您还安装了它)。
推送至 PyPI:
python3.5 setup.py register -r pypitest
python3.5 setup.py sdist upload -r pypitest
pipping 似乎与 3.6 开箱即用,运行良好。
解决方案 12:
确保你没有SSL_CERT_FILE
设置环境变量。我遇到了同样的问题,花了一段时间才弄清楚一些应用程序在我的 bash 配置文件中将此变量设置为空字符串。
解决方案 13:
我在使用 macOS Big Sur 时遇到了同样的问题。以下是我解决问题的方法。
IDE——Pycharm
下载的 Python 版本 - 3.9.6
下载最新的python版本并删除早期版本
安装时,在最后(摘要部分),有一个小提示,要求安装 SSL 证书。请仔细阅读摘要部分。
双击摘要中提供的 SSL 证书路径
它将带您进入相应的文件夹,其中应该有一个名为“安装证书命令”的文件
双击该文件,它将打开终端并自动运行代码。等到您收到“[已成功完成]”消息。
完成后,关闭并重新启动终端/IDE,这应该可以解决您的问题。
注意:如果您的系统上同时安装了 anaconda 和 python,请检查您在 IDE 中使用的是否是正确的 python 版本,该 IDE 是下载的最新版本,而不是从 Anaconda 下载的版本。
享受。
解决方案 14:
由于公司 SSL 网关,我需要将我的公司 CA 证书添加到 python CA 证书文件中。
CA 证书文件的位置可以通过以下方式找到
import ssl
print(ssl.get_default_verify_paths().openssl_cafile)
要添加的证书在钥匙串中。导出后,需要将其转换为 PEM 格式,以便可以将其剪切并粘贴到 cacert 文件中 -
openssl x509 -inform der -in cert.cer -out cert.pem
解决方案 15:
对我来说,这是请求的错误指定。我打了https
电话而不是http
打电话。改用 http 解决了这个问题。
解决方案 16:
有时如果您使用 conda 或 poetry,您可能处于虚拟环境 shell 中。您可以使用以下命令进行检查:
which python
对我来说,解决方案很简单,cmd+t
只需打开一个新 shell 即可。
解决方案 17:
如果您使用的是 macOS,请打开 finder 并转到应用程序>Python3.7 文件夹(或您正在使用的任何版本的 Python)> 双击“Install Certifications.command”文件。
解决方案 18:
只需在 Mac 上重新安装 Python