请求模块的 PyInstaller SSL 错误 - 缺少模块 ssl (_ssl)

PyInstaller SSL error with requests module - missing module ssl (_ssl)

在我的 python 脚本中,我使用请求模块调用 API 来获取和 POST 数据。

Python环境:anaconda3,python3.7/包:requests 2.24.0,pyinstaller 3.6,openssl 1.1.1h...

我对 PyInstaller 生成的 EXE-file 有以下问题:当我 运行 这个文件时,我收到以下错误消息。当我 运行 我的脚本来自 Python:

时,不会发生此错误
Traceback (most recent call last):
  File "site-packages\PyInstaller\loader\rthooks\pyi_rth_certifi.py", line 13, in <module>
  File "c:\programdata\anaconda3\envs\...\lib\site-packages\PyInstaller\loader\pyimod03_importers.py", line 623, in exec_module
  File "ssl.py", line 98, in <module>
ImportError: DLL load failed: The specified module could not be found.
[20188] Failed to execute script pyi_rth_certifi

如果我遵循例外错误行 (ssl.py):

import _ssl             # if we can't import it, let the error propagate

为什么不能导入ssl?

我在几个 post 中搜索了很长时间,但所有这些答案都没有帮助,例如:

Python Requests throwing SSLError

Twilio Python Module Errors After Compiling

Fixing SSL certificate error in exe compiled with py2exe (or PyInstaller)

有人知道如何解决这个问题吗? 如果您需要更多信息,请发表评论。谢谢


编辑:(针对 Mooncrater 的评论)

添加了来自 python 控制台的屏幕截图,输入:

import _ssl


编辑 2:

从 SO 问题中测试了“FIX”:

-> 这没有解决我的问题


编辑 3:cbolwerk 的回答

感谢您的回答,这有效!

另一个修复是安装最新的 OpenSSL Lib,我在我的 python 脚本中使用了 1.1.1h 包,在我的 PC 上安装了一个旧版本,这也导致了错误。

我在未安装 OpenSSL 的 PC 上测试了 cbolwerk 的回答,正如我所写的那样,它有效!

如果您可以在您的环境中的 python 中导入 ssl,则意味着 ssl 模块可能在您的环境中。 mentions the files you can look for inside your environment. They are either inside your env/Library/bin or env/DLLs. I think you have these installed, but pyinstaller doesn't recognize them. To make sure that pyinstaller knows these files, you can add them to datas. This can be edited in the command when building the .exe file or in the .spec file that is probably created when you have run this command once. This link 在那里可能会有用。

简而言之,将我提到的 DLL 的路径添加到数据(或实际上是 .spec 中的二进制文件),以便 pyinstaller 能够识别它们。

所以 运行 命令

pyinstaller --onedir --add-data libcrypto-1_1-x64.dll;. --add-data libssl-1_1-x64.dll;. myscript.py

或将您的 .spec 更改为

datas = [('/path/to/libcrypto-1_1-x64.dll', '.'), ('/path/to/libssl-1_1-x64.dll', '.'),
          (...) ]
...
a = Analysis(...,
             datas=datas,
             ...)
...

然后 运行

pyinstaller --onedir myscript.spec

这至少对我来说解决了 DLL 问题。