【发布时间】:2018-03-22 13:15:09
【问题描述】:
我想在 Windows 10 和 Ubuntu 中通过 PyInstaller 使用 QML(用于 Material 主题)构建 PyQt5 GUI 应用程序的可移植可执行文件(至少一个文件夹中的所有源文件)。但是,在成功构建可执行文件后,它会崩溃并显示一些错误消息。
material.py:
import os
import sys
from PyQt5.QtWidgets import QApplication
from PyQt5.QtCore import QUrl
from PyQt5.QtQml import QQmlApplicationEngine
if __name__ == "__main__":
app = QApplication(sys.argv)
os.environ["QT_QUICK_CONTROLS_STYLE"] = "Material"
engine = QQmlApplicationEngine()
engine.load(QUrl('basic.qml'))
sys.exit(app.exec_())
basic.qml:(复制自here)
import QtQuick 2.0
import QtQuick.Controls 2.1
import QtQuick.Controls.Material 2.1
ApplicationWindow {
visible: true
Material.theme: Material.Dark
Material.accent: Material.Purple
Column {
anchors.centerIn: parent
RadioButton { text: qsTr("Small") }
RadioButton { text: qsTr("Medium"); checked: true }
RadioButton { text: qsTr("Large") }
}
}
我使用以下命令来构建可执行文件:
pyinstaller ./material.py --onefile
构建后,可执行文件会显示错误消息。在 Windows 10 中:
QQmlApplicationEngine failed to load component
file:///D:/test/dist/basic.qml:1 plugin cannot be loaded for module "QtQuick": Cannot load library D:\test\dist\QtQuick.2\qtquick2plugin.dll: ???????w?????C
在 Linux 中:
QQmlApplicationEngine failed to load component
file:///media/username/EA9E5E009E5DC5AB/test/dist/basic.qml:1 plugin cannot be loaded for module "QtQuick": Cannot load library /media/username/EA9E5E009E5DC5AB/test/dist/QtQuick.2/libqtquick2plugin.so: (/usr/lib/x86_64-linux-gnu/libQt5Quick.so.5: symbol _ZN3QV46Object11markObjectsEPNS_4Heap4BaseEPNS_15ExecutionEngineE, version Qt_5_PRIVATE_API not defined in file libQt5Qml.so.5 with link time reference)
项目的文件树为:
. (test)
+-- build
| +-- (some files generate by PyInstaller)
+-- dist
| +-- QtQuick (the folder copied from Python site-packages)
| | +-- (some files copied form Python site-packages)
| +-- QtQuick.2 (the folder copied from Python site-packages)
| | +-- plugins.qmltypes
| | +-- qmldir
| | +-- qtquick2plugin.dll (or 'libqtquick2plugin.so' in Linux)
| +-- basic.qml
| +-- material.exe (or 'material' in Linux)
+-- basic.qml
+-- material.py
+-- material.spec
我复制了两个文件夹,QtQuick 和 QtQuick.2,因为我遇到了与此 question 相同的问题,并且我执行与 answer 相同的操作。我一直在寻找解决方案一个星期,不知道为什么它无法加载库。
【问题讨论】:
-
当您使用
--onefile选项时,不需要传递.so 或.dll,在我的情况下,我已经完成了测试并且它可以正常工作。你用的是什么版本的pyinstaller? -
@eyllanesc 我正在使用 PyInstaller 3.3。我不知道你是什么意思,没有必要通过
.so或.dll来使用--onefile选项。如果我不移动我的可执行文件旁边的QtQuick和QtQuick.2这两个文件夹,程序将显示模块“QtQuick.Controls”、“QtQuick”和其他在basic.qml中导入的东西没有安装。例如,file:///media/username/EA9E5E009E5DC5AB/test/dist/basic.qml:2 module "QtQuick.Controls" is not installed. -
另外,关于更多信息,我使用的 PyQt 版本是 5.10.1 和 Python 3.6。
标签: python-3.x qml pyqt5 pyinstaller qtquick2