【问题标题】:PyInstaller SSL error with requests module - missing module ssl (_ssl)请求模块的 PyInstaller SSL 错误 - 缺少模块 ssl (_ssl)
【发布时间】:2021-02-12 18:52:26
【问题描述】:

在我的 python 脚本中,我使用 requests 模块调用 API 来获取和发布数据。

Python 环境:anaconda3、python 3.7 / 包:requests 2.24.0、pyinstaller 3.6、openssl 1.1.1h...

PyInstaller 生成的 EXE 文件存在以下问题:运行此文件时,我收到以下错误消息。当我从 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?

我在 SO 上的几篇帖子中搜索了很长时间,但所有这些答案都没有帮助,例如:

Python Requests throwing SSLError

Python Requests - How to use system ca-certificates (debian/ubuntu)?

Twilio Python Module Errors After Compiling

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

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


编辑:(对于 Mooncrater 的评论)

从 python 控制台添加截图,输入:

import _ssl


编辑 2:

从 SO Question 测试了“FIX”: Python SSL Import Error in PyInstaller generated executable

->这并没有解决我的问题


编辑 3:cbolwerk 回答

感谢您的回答,这行得通!

Python 3.7 anaconda environment - import _ssl DLL load fail error

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

cbolwerk 的答案我在没有安装 OpenSSL 的 PC 上测试过,正如我所写,它可以工作!

【问题讨论】:

  • @Mooncrater 我尝试了一个旧的python版本,同样的问题!
  • @droebi 您能否在创建 EXE 时尝试在 Python 3.6.8 中添加--hidden-import pyodbc?取自this 答案。我之前也遇到过类似的问题。
  • @Mooncrater 我试过这个修复,同样的问题 :( ...但是非常感谢你到目前为止的支持
  • 嗯,这很奇怪。我认为,特别是出于这个原因,我坚持使用 Python 3.6.8 + Pyinstaller 组合。也许您也应该在 pyinstaller 的 repo 页面上提出问题。这可能会导致一些事情。
  • @Mooncrater 是的,奇怪的是这个词的正确说法......

标签: python ssl python-requests openssl pyinstaller


【解决方案1】:

如果你的环境中可以在python中导入ssl,则说明ssl模块很可能在你的环境中。 This answer 提到了您可以在环境中查找的文件。它们位于您的 env/Library/bin 或 env/DLL 中。我认为您已经安装了这些,但 pyinstaller 无法识别它们。为了确保 pyinstaller 知道这些文件,您可以将它们添加到 datas.xml 中。这可以在构建 .exe 文件时在命令中进行编辑,或者在您运行此命令一次时可能创建的 .spec 文件中进行编辑。 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 至少对我来说是个问题。

【讨论】:

  • 谢谢回复,我试试看。
  • 在我的 SPEC 文件中绑定 DLL
猜你喜欢
  • 2022-01-18
  • 1970-01-01
  • 1970-01-01
  • 2020-11-18
  • 2020-01-30
  • 1970-01-01
  • 2011-12-25
  • 1970-01-01
  • 2018-04-03
相关资源
最近更新 更多