【问题标题】:"Failed to execute script" when trying to run .exe file created by pyinstaller尝试运行 pyinstaller 创建的 .exe 文件时“执行脚本失败”
【发布时间】:2020-01-23 10:39:01
【问题描述】:

我正在尝试使用 pyinstaller 将我的 python 代码编译为 .exe 文件。 使用以下代码会生成一个没有错误消息的可用 .exe 文件:

from PyQt5 import QtCore, QtGui, QtWidgets
import sys
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = QtWidgets.QMainWindow()
    window.show()
    print('app loaded')
    sys.exit(app.exec_())

但是,只要我添加:

from scipy.integrate import solve_ivp

对于导入语句,我收到错误消息“无法执行脚本”。 在命令窗口中执行 pyinstaller 期间,我还看到以下内容:“警告:无法找到 Qt5 翻译”以及包括 Matplotlib 模块在内的几个警告。 我尝试将 PyQt5\Qt\plugins\platforms 文件夹复制到保存 .exe 文件的文件夹中,并将 Anaconda3\Library\plugins 添加为环境变量 QT_PLUGIN_PATH,如另一个问题中所建议的那样,但两种建议的解决方案均无效。
当我尝试从命令窗口执行程序时,我收到错误消息:“ImportError: DLL load failed”
对此问题的任何帮助将不胜感激。

编辑:
我尝试了 oetzi 的提议,并专门为我的项目创建了一个新的 anaconda 环境。我安装了 scipy、numpy、matplotlib 和 pyqt 的模块,并在那个新环境中安装了 pyinstaller。这次我在 Anaconda Prompt 中输入了命令。同样,没有 scipy.integrate.solve_ivp 它可以正常工作。有了它,当我尝试运行 pyinstaller 时出现以下错误。

18881 WARNING: Cannot read QLibraryInfo output: raised Expecting value: line 1 column 1 (char 0) when decoding:
False
Traceback (most recent call last):
  File "C:\Users\domin\Anaconda3\envs\testenv\Scripts\pyinstaller-script.py", line 10, in <module>
    sys.exit(run())
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\__main__.py", line 111, in run
    run_build(pyi_config, spec_file, **vars(args))
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\__main__.py", line 63, in run_build
    PyInstaller.building.build_main.main(pyi_config, spec_file, **kwargs)
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\building\build_main.py", line 844, in main
    build(specfile, kw.get('distpath'), kw.get('workpath'), kw.get('clean_build'))
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\building\build_main.py", line 791, in build
    exec(code, spec_namespace)
  File "C:\Users\domin\polybox\ETH\Master Thesis\PycharmProjects\Perfusion Simulation\Pyinstaller\test\test.spec", line 17, in <module>
    noarchive=False)
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\building\build_main.py", line 243, in __init__
    self.__postinit__()
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\building\datastruct.py", line 158, in __postinit__
    self.assemble()
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\building\build_main.py", line 502, in assemble
    module_hook.post_graph()
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\building\imphook.py", line 410, in post_graph
    self._load_hook_module()
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\building\imphook.py", line 377, in _load_hook_module
    self.hook_module_name, self.hook_filename)
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\compat.py", line 793, in importlib_load_source
    return mod_loader.load_module()
  File "<frozen importlib._bootstrap_external>", line 407, in _check_name_wrapper
  File "<frozen importlib._bootstrap_external>", line 907, in load_module
  File "<frozen importlib._bootstrap_external>", line 732, in load_module
  File "<frozen importlib._bootstrap>", line 265, in _load_module_shim
  File "<frozen importlib._bootstrap>", line 696, in _load
  File "<frozen importlib._bootstrap>", line 677, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 728, in exec_module
  File "<frozen importlib._bootstrap>", line 219, in _call_with_frames_removed
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\hooks\hook-PySide2.py", line 18, in <module>
    collect_system_data_files(pyside2_library_info.location['PrefixPath'],
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\utils\hooks\qt.py", line 87, in __getattr__
    return getattr(self, name)
  File "C:\Users\domin\Anaconda3\envs\testenv\lib\site-packages\PyInstaller\utils\hooks\qt.py", line 89, in __getattr__
    raise AttributeError
AttributeError

【问题讨论】:

  • 我尝试使用您共享的代码和 pyinstaller v 3.5。在这两种情况下都可以使用 scipy.integrate import solve_ivp 或不使用。这可能是您用于项目的 anaconda 环境的一部分。如果您使用的是以前的版本,请使用 pyinstaller v 3.5 重试。如果它不起作用,请尝试使用项目特定的 anaconda 环境。
  • 使用dependency walker 知道缺少哪些dll,然后搜索它们并将它们粘贴到可执行文件旁边
  • 我在调试模式下运行 pyinstaller,我认为最重要的警告是以下内容:WARNING: Unable to find Qt5 translations c:/users/domin/appdata/local/programs/python/python37-32/lib/site-packages/PyQt4/translations\qtbase_*.qm. These translations were not packaged. 我不太清楚为什么在我使用 PyQt5 时它需要来自 PyQt4 的任何文件。我已经安装了 PyQt4,但它正在寻找的文件不存在。但是为什么它会在 PyQt4 文件夹中寻找 Qt5 的翻译呢?
  • 好吧我直接把Qt5的翻译全部复制到Qt4对应的文件夹下,就去掉了警告信息。现在,当我启动我的 .exe 文件时,命令提示符会打开一小会儿,然后立即关闭,并且我的 MainWindow 小部件未显示
  • 我尝试了@eyllanesc 提出的依赖遍历器,但在我的 xref_test.html 文件中没有提到任何丢失的 dll 文件,只有一堆丢失的模块。我尝试安装其中一些,但这只会引发其他问题

标签: python python-3.x pyqt5 pyinstaller


【解决方案1】:

好的,我现在所做的只是完全卸载并重新安装所有内容。之后我只添加了 scipy、numpy、matplotlib、pyqt5 和 pyinstaller 所需的模块。之后,一切正常。我的猜测是,因为我同时安装了 PyQt5 和 PyQt4,所以它与模块混淆了。现在我只安装了 PyQt5,一切正常。

【讨论】:

    猜你喜欢
    • 2018-12-09
    • 1970-01-01
    • 1970-01-01
    • 2022-06-19
    • 1970-01-01
    • 2016-12-14
    • 1970-01-01
    • 2020-11-07
    • 1970-01-01
    相关资源
    最近更新 更多