You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何解决Mac系统运行Python时出现的ssl.SSLCertVerificationError报错

Mac Python 3.9 SSLCertVerificationError 解决方案
  • 方案1:运行Python内置证书安装脚本
    Mac版Python 3.9默认未关联系统根证书,需手动执行自带安装脚本:
    1. 打开Finder进入「应用程序」→「Python 3.9」目录
    2. 双击运行Install Certificates.command等待执行完成
      若双击报错可直接在终端执行命令:
      open "/Applications/Python 3.9/Install Certificates.command"
  • 方案2:代码中显式指定证书路径
    执行完方案1仍无效可在请求逻辑中直接指定证书路径,常见场景示例:
    • requests库请求
      单次请求指定官方证书库:
    import requests
    import certifi
    
    resp = requests.get("https://cognito-idp.us-west-2.amazonaws.com/us-west-2_OnQppLqYQ....", verify=certifi.where())
    
    临时测试用可关闭验证(生产环境禁止使用):
    import requests
    requests.packages.urllib3.disable_warnings()
    resp = requests.get("https://cognito-idp.us-west-2.amazonaws.com/us-west-2_OnQppLqYQ....", verify=False)
    
    • 标准库urllib请求
    import urllib.request
    import ssl
    import certifi
    
    context = ssl.create_default_context(cafile=certifi.where())
    resp = urllib.request.urlopen("https://cognito-idp.us-west-2.amazonaws.com/us-west-2_OnQppLqYQ....", context=context)
    
  • 方案3:添加自定义根证书到Python证书库
    如果你处于公司代理、内网等需要自定义根证书的网络环境,可将自定义pem格式证书追加到Python证书文件中:
    1. 执行命令获取Python证书文件路径:
      python3.9 -c "import certifi; print(certifi.where())"
    2. 将自定义根证书的内容追加到上述路径的文件末尾即可
  • 方案4:配置全局环境变量
    将以下两行配置写入~/.zshrc(bash用户写入~/.bash_profile):
    export REQUESTS_CA_BUNDLE=/etc/ssl/cert.pem
    export SSL_CERT_FILE=/etc/ssl/cert.pem
    
    执行source ~/.zshrc(bash用户执行source ~/.bash_profile)后生效。

内容的提问来源于stack exchange,提问作者reza

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 01:15:06