【问题标题】:brew installation of Python 3.6.1: [SSL: CERTIFICATE_VERIFY_FAILED] certificate verify failedbrew安装Python 3.6.1:[SSL:CERTIFICATE_VERIFY_FAILED]证书验证失败
【发布时间】:2017-11-22 18:49:58
【问题描述】:

我使用

安装了 python 3.6

brew install python3

并尝试从 https 下载带有six.moves.urllib.request.urlretrieve 的文件,但它会引发错误

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

在Python安装(来自.pkg)中,README指出需要在安装to后运行Install Certificates.command

  1. 安装certifi
  2. 将认证路径符号链接到certify 路径

能够使用证书。

但是,在 brew install 中,该文件不存在,并且似乎没有运行。

【问题讨论】:

  • 在终端中执行 /Applications/Python\ 3.6/Install\ Certificates.command 解决了我的自制 python3 安装问题。
  • 鉴于这个问题的受欢迎程度,我回复了a issue to brew。
  • export SSL_CERT_DIR=/etc/ssl/certs 在 Mac OS Big Sur 为我工作。

标签: python ssl


【解决方案1】:

似乎出于某种原因,Brew 没有运行 Mac 的 Python3 捆绑包中的 Install Certificates.command。这个问题的解决方法是在brew install python3之后运行下面的脚本(复制自Install Certificates.command):

# 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)

    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()

【讨论】:

  • 如果一个人没有 root 访问权限/sudo 权限,该怎么办?谢谢。
  • 我运行了上面的命令,它似乎工作成功,但我仍然收到错误。关于如何调试的任何想法?
  • 供参考,这个脚本的来源在这里:github.com/python/cpython/blob/master/Mac/BuildScript/resources/…
  • @tommy.carstensen 你只需要为你自己的虚拟环境运行它
  • 我必须将import ssl; ssl._create_default_https_context = ssl._create_stdlib_context 添加到我的代码中才能正常工作。
【解决方案2】:

我的 Mac OS X 解决方案:

1) 使用从Python语言官方网站https://www.python.org/downloads/下载的本机应用Python安装程序升级到Python 3.6.5

我发现这个安装程序在更新新 Python 的链接和符号链接方面比自制软件好得多。

2) 使用刷新后的 Python 3.6 目录中的“./Install Certificates.command”安装新证书

cd "/Applications/Python 3.6/" sudo "./Install Certificates.command"

【讨论】:

  • 谢谢!为我工作,我在 3.8 上(将命令适当地更改为 Python 3.8 目录)。
  • 我的应用程序中没有 Python 文件夹,我无法在我的机器上找到它。有什么想法可以解决我的问题吗?
  • @talha06 - 为了知道 Python 在你的计算机上的位置,打开 python shell 输入python 或python3 然后import sys 和print('\n'.join(sys.path)) 或者你可以输入这个命令终端python -c "import sys; print('\n'.join(sys.path))"
  • 感谢对我所做的微小改动:$ cd /Applications/Python 3.8/$ sudo ./Install\ Certificates.command快乐编码:)
【解决方案3】:
  • 找出默认的cafile:
python -c 'import ssl; print(ssl.get_default_verify_paths().openssl_cafile)'

/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/etc/ssl/cert.pem

sudo mkdir -p /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/etc/ssl/certs
  • 找出certifi的ca文件
python -c 'import certifi; print(certifi.where())'

'/usr/local/lib/python3.7/site-packages/certifi/cacert.pem'

  • 复制到
sudo cp /usr/local/lib/python3.7/site-packages/certifi/cacert.pem
/Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.7/etc/ssl/certs/cert.pem

【讨论】:

  • 这在 conda python 环境中对我不起作用,使用 conda python。我尝试将证书复制到三个不同的目录中:ssl、ssl/crt、etc/ssl/certs。到目前为止我还没有找到解决方案。
【解决方案4】:

对于临时,以下将禁用 ssl 检查,

import ssl
ssl._create_default_https_context = ssl._create_unverified_context

【讨论】:

    【解决方案5】:

    如果您需要使本地根证书(例如local_RootCA.crt)被python信任,您可以将其添加到certifi/cacert.pem文件的末尾:

    cat local_RootCA.crt >> `python -c 'import certifi; print(certifi.where())'`
    

    该解决方案也适用于 macos brew python 3 安装。

    【讨论】:

      猜你喜欢
      • 2018-03-15
      • 2021-03-02
      • 1970-01-01
      • 1970-01-01
      • 2023-04-02
      • 2019-10-01
      • 1970-01-01
      • 2015-06-05
      • 2016-08-06
      相关资源
      最近更新 更多