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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42098126/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 21:15:17  来源:igfitidea点击:

Mac OSX python ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

pythonmacosssl

提问by Edward Ned Harvey

Many operations in python require accessing things via https. This includes pip install, or just using http.client.HTTPSConnection, or any modules or applications that use these things internally.

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

If python was installed from the official python pkg installer, downloaded from https://python.org, then it uses an internal version of openssl, and contains no root certificates. Anything that uses an SSL connection results in this error:

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

ssl.SSLError: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:749)

How can I install root certs to make the above error go away?

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

回答by Edward Ned Harvey

When you run the python installer, they display this information to you. It is also documented in /Applications/Python 3.6/ReadMe.rtf, but it's very easily overlooked.

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

Just browse to Applications/Python 3.6and double-click Install Certificates.command

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

There is an issue in the Python bug tracker about this. http://bugs.python.org/issue29480

Python 错误跟踪器中存在有关此问题的问题。 http://bugs.python.org/issue29480

Update:This issue is marked as resolved in the bug tracker with this comment being the latest one:

更新:此问题在错误跟踪器中标记为已解决,此评论是最新评论:

For 3.7.0b2, I have tried to make things more obvious in two ways. One, the installer package will now attempt to open a Finder window for the /Application/Python 3.7 folder that contains the "Install Certificates.command". Two, rather than just a generic "installation complete" message at the end of the install, there is now a tailored message that urges the user to click on the "Install Certificates.command" icon.

对于 3.7.0b2,我试图通过两种方式使事情变得更加明显。一,安装程序包现在将尝试为包含“Install Certificates.command”的 /Application/Python 3.7 文件夹打开一个 Finder 窗口。第二,安装结束时不仅仅是一条通用的“安装完成”消息,现在还有一条量身定制的消息,敦促用户单击“安装 Certificates.command”图标。

回答by hyung ook An

I solved this problem using this command:

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

open /Applications/Python\ 3.7/Install\ Certificates.command

I have Python 3.7 in my machine.

我的机器中有 Python 3.7。

Check this link - Fixing CERTIFICATE_VERIFY_FAILED error when trying requests-html out on Mac

检查此链接 -在 Mac 上尝试 requests-html 时修复 CERTIFICATE_VERIFY_FAILED 错误

回答by Oded BD

Cool way to solve this issue for all your python version and without checking your version on macOS

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

bash /Applications/Python*/Install\ Certificates.command

This command is equivalent to:

此命令等效于:

...
bash /Applications/Python\ 2.7/Install\ Certificates.command
bash /Applications/Python\ 3.6/Install\ Certificates.command
bash /Applications/Python\ 3.7/Install\ Certificates.command
...

It helped me hope it will help you as well

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

回答by Daksh Shah

In my case none of the solutions worked with the system installed python3 in macOS Catalina, neither did it work with python3 installed via brew.

在我的情况下,没有一个解决方案适用于在 macOS Catalina 中安装 python3 的系统,也不适用于通过 brew 安装的 python3。

If someone has a situation like this and wants a quick solution,
Download and install python3 again, using https://www.python.org/downloads/

如果有人遇到这种情况并想要快速解决方案,
请使用https://www.python.org/downloads/再次下载并安装 python3

At the end of the installation, the installer would show you a note, asking to run the Install Certificates.commandfile.
(With the other installations, this file was not present, and neither was the solutionwith the file's source code working)

在安装结束时,安装程​​序会向您显示一条说明,要求运行该Install Certificates.command文件。
(对于其他安装,此文件不存在,文件源代码的解决方案也不存在)

Restart the terminal, and you can type where python3, to see /Library/Frameworks/Python.framework/Versions/3.8/bin/python3. Using this binary, the problem should not occur.

重新启动终端,您可以键入where python3, 以查看/Library/Frameworks/Python.framework/Versions/3.8/bin/python3。使用这个二进制文件,应该不会出现问题。

Note: It might be possible to make the system-installed python3 work, but in my case; it proved to be extremely hard, so I choose this way.

注意:有可能使系统安装的 python3 工作,但在我的情况下;事实证明它非常困难,所以我选择了这种方式。

回答by Andy Wang

If pip does not fix the issue

如果 pip 没有解决问题

pip3 install --upgrade certifi

Then try the following scripts if you can't find the "Install Certificates.command"

如果找不到“ 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()

回答by Ratha Pech

I faced the same problem, when I tried to run Python with Keras data loading. The error for me was:

当我尝试使用 Keras 数据加载运行 Python 时,我遇到了同样的问题。我的错误是:

Exception: URL fetch failure on AWS_URL: None -- [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failed (_ssl.c:833)

I fixed my problem by upgrading the certificate as:

我通过将证书升级为以下内容来解决我的问题:

pip install --upgrade certifi

回答by jarekwg

A cheap way around this is just using python3.5 if you still have it installed.

如果您仍然安装了python3.5,那么一种廉价的方法就是使用python3.5。

Pushing to PyPI:

推送到 PyPI:

python3.5 setup.py register -r pypitest

python3.5 setup.py register -r pypitest

python3.5 setup.py sdist upload -r pypitest

python3.5 setup.py sdist upload -r pypitest

pipping seems to work fine with 3.6 out of the box..

开箱即用的 3.6 似乎可以正常工作。

回答by pt_arun

If you're using macOS open finder and go to Applications > Python3.7 folder (or whatever version of python you're using) > double click on "Install Certificates.command" file.

如果您使用的是 macOS,请打开 finder 并转到应用程序 > Python3.7 文件夹(或您使用的任何版本的 Python)> 双击“安装 Certificates.command”文件。