python - 操作系统错误 : Could not find a suitable TLS CA certificate bundle

标签 python ssl python-requests pycharm ca

在向 binance 发出 Api 请求时,在使用 pyinstaller 将 main.py 构建为 exe 后,我收到此错误:

OSError: Could not find a suitable TLS CA certificate bundle, invalid path: C:\Users\ADMINI~1\AppData\Local\Temp\2\_MEI76602\certifi\cacert.pem 
在我运行机器人的服务器上,在本地机器上:
OSError: Could not find a suitable TLS CA certificate bundle, invalid path: C:\Users\USER\PycharmProjects\pythonproject2\dist\cacert.pem 
两者的错误的确切位置是:
File "requests\adapters.py", line 227, in cert_verify
在服务器上,该程序已经运行了一个多星期,没有任何问题,直到 2 天前我开始收到 OSError。但只有 API 调用导致程序仍在运行时出现错误,websocket 仍在接收数据。
我尝试在 python requests can't find a folder with a certificate when converted to .exe 的已接受答案中添加代码
我也试过:
pip config set global.cert "path\to\cacert.pem"
使用正确的 cacert.pem 路径,使用
python -c "import certifi; print(certifi.where())"
这些都不起作用,目前我发现的唯一解决方法是将 cacert.pem 文件手动复制到 OSError 说它正在寻找它的位置(cacert.pem 文件)。
但我以前从来没有这样做过。最近我更新了我的 python 版本,然后由于 websocket 问题再次降级。我通过完全卸载 python 3.10 并从 PATH 中删除所有引用然后重新安装 3.9.7 并添加所有 PATH 变量来做到这一点。
有谁知道是什么原因造成的?当我在 pycharm 本身中运行脚本时,我没有收到此错误,我还注意到 global.cert 与本地计算机上的 OSError 路径不同,但我不知道如何从 cacert.pem 文件中更改它的外观.

最佳答案

好的,所以现在我找到了一个临时解决方案。同样发生的事情是程序在寻找 cacert.pem 文件的地方,没有这样的文件。所以我找到了解决方法:
在您的项目终端中,转到 External Libraries -> Python3.9 -> site-packages -> certifi -> core.py
然后查找此代码:

import os

try:
    from importlib.resources import path as get_path, read_text

    _CACERT_CTX = None
    _CACERT_PATH = None

    def where():
    .............
现在,当您运行程序时,获取它在抛出错误时查找 cacert.pem 的路径。将 cacert.pem 文件粘贴到 Temp 文件夹或 Temp\2 等。然后复制路径。
将代码更改为:
import os

try:
    from importlib.resources import path as get_path, read_text

    _CACERT_CTX = None
    _CACERT_PATH = "copied path"

    def where():
    .............
另外,我在新系统上编译了完全相同的代码,一切正常。所以很可能问题出在我安装和卸载 3.10 时。如果我找到确切的原因会更新。

关于python - 操作系统错误 : Could not find a suitable TLS CA certificate bundle,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70094634/

相关文章:

Python:从导入的 css 文件中查找宽度

python django-import-export 即使没有数据也导入模型数据

wordpress - 从 wordpress htaccess 重定向到 http 到 https

python - 操作系统错误: [Errno 22] Invalid argument writing tar file

python - 如何删除普通表格中的链接?

python - 创建多个 PhantomJS 实例还是只创建一个?

Python3 帮助确定动态创建列表的大多数 pythonic 方法

python - 如何在 python 中验证 SSL 证书?

amazon-web-services - 使用 AWS ELB 为带有自定义 ACM 证书的 HTTPS 流量配置 Istio Ingress

python - request.get(url) 返回空内容