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

时间:2020-10-30 09:59:47

标签: python ssl python-requests openssl pyinstaller

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

Python环境:anaconda3,python 3.7 /软件包:请求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)

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


编辑:(供Mooncrater评论)

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

import _ssl

import _ssl


编辑2:

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

->这不能解决我的问题


编辑3:cbolwerk回答

THX为您的答案,有效!

Python 3.7 anaconda environment - import _ssl DLL load fail error

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

我在没有安装OpenSSL的PC上测试了cbolwerk的答案,而且正如我所写的那样,它可以正常工作!

1 个答案:

答案 0 :(得分:1)

如果可以在环境中的python中导入ssl,则意味着ssl模块可能在您的环境中。 This answer提到您可以在环境中查找的文件。它们位于您的env / Library / bin或env / DLL中。我认为您已经安装了这些工具,但是pyinstaller无法识别它们。为了确保pyinstaller知道这些文件,可以将它们添加到数据中。可以在构建.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问题至少对我来说有用。