【问题标题】:PyInstaller generated exe file error : qt.qpa.plugin: could not load the qt platform plugin "windows" in "" even though it was foundPyInstaller 生成 exe 文件错误:qt.qpa.plugin: could not load the qt platform plugin "windows" in "" 即使找到了
【发布时间】:2020-05-06 09:56:09
【问题描述】:

我创建了一个程序来从驱动器上的文件中读取某些数据,在 PyQt5 ui 上显示结果并接受用户的更正。

当作为 python 文件运行时,该程序运行良好。但是,当我使用 PyInstaller 将其转换为独立的 exe 时,它​​可以正常工作,直到需要启动 pyqt5 gui。此时,它停止抛出以下错误:

qt.qpa.plugin: 无法在“”中加载 Qt 平台插件“windows”,即使它已找到。此应用程序无法启动,因为无法初始化 Qt 平台插件。重新安装应用程序可能会解决此问题。可用的平台插件有:minimal、offscreen、windows。

我已阅读 thisthisthis,但它们并没有解决我的问题。

gui的代码很大,但结构如下:

from PyQt5 import uic, QtWidgets
import sys
import os

#baseUIClass, baseUIWidget = uic.loadUiType('gui.ui')
baseUIClass, baseUIWidget = uic.loadUiType(r'C:\mypath\gui.ui')

class Ui(baseUIClass, baseUIWidget ):

    def __init__(self, *args, **kwargs):
        baseUIWidget.__init__(self, *args, **kwargs)
        self.setupUi(self)

# More code to perform the desired actions

def run(input_variables):
    app = QtWidgets.QApplication(sys.argv)
    ui = Ui()
    ui.show()
# More code to make the Ui perform desired actions
    app.exec_()
    return(output_variables)

代码已转换为带有以下参数的独立 exe:

pyinstaller --hiddenimport <hidden import> --onefile <python filename>

请问您知道如何解决这个问题吗?

谢谢

【问题讨论】:

  • 使用dependencywalker检查是否缺少任何dll。

标签: python python-3.x pyqt pyqt5 pyinstaller


【解决方案1】:

我在编译的应用程序中遇到了同样的错误消息。

为了追查这个问题,我首先将应用程序剥离到它的骨架中,并使用最少的导入,它在编译时可以完美运行。然后我部分添加了我的“大”应用程序的所有导入,直到错误再次出现。

最后(至少对我而言)熊猫是罪魁祸首:

最小的可重复示例,这很有效:

from PyQt5 import QtWidgets

app = QtWidgets.QApplication([])
window = QtWidgets.QMainWindow()
window.show()
app.exec()

这(在第 2 行添加了 pandas 导入)引发了您描述的错误:

from PyQt5 import QtWidgets
import pandas as pd  # import after pyqt5

app = QtWidgets.QApplication([])
window = QtWidgets.QMainWindow()
window.show()
app.exec()

但是,当第一次导入 pandas 然后再导入 PyQt 5 时,我的编译版本又可以工作了:

import pandas as pd  # import before pyqt5
from PyQt5 import QtWidgets

app = QtWidgets.QApplication([])
window = QtWidgets.QMainWindow()
window.show()
app.exec()

因此,在我的情况下,解决方案是追踪“错误的导入”,摆弄进口订单并获得幸运(我太缺乏经验,甚至无法理解为什么进口订单会导致此错误)

如果您没有使用 pandas,也许整个“剥离到骨架并开始逐个导入”的方法将帮助您进一步阐明错误的根本原因。

如果您正在使用 pandas 并且顺序切换没有帮助,there is another thread 描述了哪些尝试处理 pandas 编译问题。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2015-12-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2023-01-02
    相关资源
    最近更新 更多