Mac OSX python ssl.SSLError:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败(_ssl.c:749)

Edw*_*vey 51 python macos ssl

python中的许多操作都需要通过https访问.这包括pip安装,或仅使用http.client.HTTPSConnection,或内部使用这些内容的任何模块或应用程序.

如果python是从官方的python pkg安装程序安装的,从https://python.org下载,那么它使用的是openssl的内部版本,并且不包含根证书.任何使用SSL连接的内容都会导致此错误:

ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)
Run Code Online (Sandbox Code Playgroud)

如何安装根证书以使上述错误消失?

Edw*_*vey 140

运行python安装程序时,它们会向您显示此信息.它也有记录/Applications/Python 3.6/ReadMe.rtf,但很容易被忽视.

只需浏览Applications/Python 3.6并双击即可Install Certificates.command

Python bug跟踪器中存在一个问题. http://bugs.python.org/issue29480

  • @gma992我通过homebrew和pyenv安装了python。我在存储我的 python 版本的目录中没有看到安装证书命令。 (5认同)
  • 我运行了Install Certificates.command文件,但如果我在代码中使用https URL(读取内容),则错误仍然存​​在(在Py36中).如果我使用相同的URL字符串减去"s"(例如,http),我的代码就可以了.不幸的是,我需要https来获取某些网址.有什么建议吗?(我还运行了"pip3 install --upgrade certifi",因为我需要运行Py27和Py36) (3认同)
  • 是否有解决方案,如果没有root访问权限?谢谢. (2认同)
  • 这不起作用;正如@ultrageek 提到的那样,所有错误仍然存​​在。另外,请列出实际的命令,而不是与特定版本的 Python 捆绑在一起的别名。-1. (2认同)
  • 我的“应用程序”文件夹中没有Python (2认同)

Dav*_*ido 27

无需在 macOS 上检查您的版本,即可为您的所有 Python 版本解决此问题的酷方法

bash /Applications/Python*/Install\ Certificates.command
Run Code Online (Sandbox Code Playgroud)

此命令等效于:

...
bash /Applications/Python\ 2.7/Install\ Certificates.command
bash /Applications/Python\ 3.6/Install\ Certificates.command
bash /Applications/Python\ 3.7/Install\ Certificates.command
...
Run Code Online (Sandbox Code Playgroud)

它帮助了我希望它也能帮助你


小智 17

如果 pip 没有解决问题

pip3 install --upgrade certifi
Run Code Online (Sandbox Code Playgroud)

如果找不到“ Install Certificates.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()
Run Code Online (Sandbox Code Playgroud)

  • 谢谢。为我工作。可能是在许多情况下唯一有效的解决方案。 (2认同)

Ahm*_* J. 11

如果您使用的是 MacOS,请转至应用程序 >> python3.8 >> 并双击安装证书.command。这对我有用。


小智 7

我使用以下命令解决了这个问题:

open /Applications/Python\ 3.7/Install\ Certificates.command
Run Code Online (Sandbox Code Playgroud)

我的机器上装有Python 3.7。

检查此链接- 在Mac上尝试请求HTML时修复CERTIFICATE_VERIFY_FAILED错误


Dak*_*hah 5

在我的情况下,没有一个解决方案适用于在 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 工作,但在我的情况下;事实证明它非常困难,所以我选择了这种方式。