Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/305.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
brew安装Python 3.6.1:[SSL:CERTIFICATE\u VERIFY\u FAILED]CERTIFICATE VERIFY失败_Python_Ssl - Fatal编程技术网

brew安装Python 3.6.1:[SSL:CERTIFICATE\u VERIFY\u FAILED]CERTIFICATE VERIFY失败

brew安装Python 3.6.1:[SSL:CERTIFICATE\u VERIFY\u FAILED]CERTIFICATE VERIFY失败,python,ssl,Python,Ssl,我使用 brew安装python3 并尝试从https下载带有six.moves.urllib.request.urlretrieve的文件,但它抛出了错误 ssl.SSLError:[ssl:CERTIFICATE\u VERIFY\u FAILED]证书验证失败(\u ssl.c:749) 在Python安装(来自.pkg)中,自述文件表明安装后需要运行Install Certificates.command 安装certifi 将认证路径符号链接到certificatepath 能够使用证

我使用

brew安装python3

并尝试从https下载带有
six.moves.urllib.request.urlretrieve
的文件,但它抛出了错误

ssl.SSLError:[ssl:CERTIFICATE\u VERIFY\u FAILED]证书验证失败(\u ssl.c:749)

在Python安装(来自.pkg)中,自述文件表明安装后需要运行
Install Certificates.command

  • 安装
    certifi
  • 将认证路径符号链接到
    certificate
    path
  • 能够使用证书


    但是,在brew安装中,此文件不存在,并且似乎没有运行。

    出于某种原因,brew似乎没有运行Python3 Mac捆绑包中的
    安装证书。此问题的解决方案是在
    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()
    

    我的Mac OS X解决方案:

    1) 使用从官方Python语言网站下载的本机应用程序Python安装程序升级到Python 3.6.5

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

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

    cd“/Applications/Python 3.6/” sudo“/Install Certificates.command”


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

    import ssl
    ssl._create_default_https_context = ssl._create_unverified_context
    
    • 查找默认文件:
    python-c'导入ssl;打印(ssl.get_default_verify_path().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;打印(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
    
    如果没有root访问权/sudo权限,该怎么办?谢谢。我运行了上面的命令,它似乎工作成功,但我仍然得到错误。关于如何调试这个有什么想法吗?作为参考,这个脚本的源代码在这里:@tommy.carstensen您只需为您自己的虚拟环境运行它,我必须添加
    导入ssl;ssl.\u create\u default\u https\u context=ssl.\u create\u stdlib\u context
    到我的代码中让它工作。在终端中执行
    /Applications/Python\3.6/Install\Certificates.command
    修复了我的自制python3安装中的这个问题。鉴于这个问题的流行性,我选择了它。谢谢!为我工作,我现在使用3.8(将命令适当地更改为Python 3.8目录)。我的应用程序中没有Python文件夹,我无法在我的计算机上找到它。有什么办法可以解决我的问题吗?@talha06-为了知道Python在您的计算机上的位置,打开Python外壳,键入
    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
    Happy Coding:)这在使用conda Python的conda Python环境中对我不起作用。我尝试在三个不同的目录中复制证书:ssl、ssl/crt等/ssl/certs。到目前为止,我还没有找到解决办法。