"ImportError: DLL load failed while importing" while using PyInstaller

"ImportError: DLL load failed while importing" while using PyInstaller

我正在尝试使用 PyInstaller 打包 PyQt5 Python 应用程序。

当我正常打包时,不使用 UPX,它工作正常。但是,当我开始使用 UPX 时,我开始 运行 遇到很多问题。我必须使用 --upx-exclude "vcruntime140.dll" 来防止该文件被损坏。

然后,我运行陷入了这个问题。

Traceback (most recent call last):
  File "main.py", line 3, in <module>
ImportError: DLL load failed while importing QtWidgets: The parameter is incorrect.
[26400] Failed to execute script main

这是main.py的开头:

import sys
import PyQt5
from PyQt5 import QtWidgets
from PyQt5.QtGui import QIcon

我读到 (我想是类似的问题),我可能在隐藏导入时遇到问题,所以我 运行 使用 --hidden-import "PyQt5" PyInstaller 命令,然后使用 --hidden-import "PyQt5" --hidden-import "QtWidgets"。但我不断收到同样的错误,DLL load failed while importing QTWidgets

我使用的完整 PyInstaller 命令是:

pyinstaller -n "[exe name]" -i "[icon file path].ico" --upx-dir "[path to UPX]\upx-3.96-win64" --upx-exclude "vcruntime140.dll" --hidden-import "PyQt5" --hidden-import "QtWidgets" --clean main.py

我该如何解决这个错误?

本周早些时候我遇到了完全相同的问题。 我通过完全禁用 UPX 来修复它。您可以通过将越来越多的 QT DLL 添加到 UPX 排除列表来确定哪些具体是损坏的,如果您仍然想压缩其中的一些,但现在我可以完全禁用它。

我尝试了很多东西 - 不同版本的 PyQT5,不同版本的其他库,none 都有任何效果。

如果您有基于文件夹构建的 .spec 文件,请确保在 exe 和 coll 部分中禁用 UPX。