PyInstaller + PyQt5 + QML: QtQuick未安装。

4

我正在尝试使用pyinstaller、PyQt5和QML构建一个应用程序(参见下面的文件),使用以下命令。

pyrcc5 pyqt5_qml.qrc > pyqt5_qml_qrc.py
pyinstaller -w -F --noupx pyqt5_qml.py

(OSX 10.11.1、Python 3.5.0、Qt 5.5.1、PyInstaller 3.0)

pyqt5_qml.py 运行正常(打开一个“Hello world!”窗口),但是构建的应用程序报错:module "QtQuick" version 2.4 is not installed。我猜测该模块未被包含在构建的应用程序中,但我不确定如何告诉 PyInstaller 做到这一点。

pyqt5_qml.py:

import os, sys
from PyQt5 import QtCore, QtWidgets, QtQml
import pyqt5_qml_qrc

def main():
    global app 
    app = QtWidgets.QApplication(sys.argv)
    engine = QtQml.QQmlApplicationEngine()
    engine.load(QtCore.QUrl('qrc:/hello.qml'))
    root = engine.rootObjects()[0]
    root.show()
    sys.exit(app.exec_())

if __name__ == "__main__":
    main()

hello.qml:

import QtQuick 2.4
import QtQuick.Controls 1.3
import QtQuick.Layouts 1.1

ApplicationWindow {
    title: qsTr("Window")
    Rectangle {
        width: 360
        height: 360
        Text {
            anchors.centerIn: parent
            text: "Hello World"
        }
        MouseArea {
            anchors.fill: parent
            onClicked: {
                Qt.quit();
            }
        }
    }
}

pyqt5_qml.qrc:

<!DOCTYPE RCC><RCC version="1.0">
<qresource>
    <file>hello.qml</file>
</qresource>
</RCC>
3个回答

1
我希望这能有所帮助。
我曾经也遇到同样的问题。
经过几个小时的尝试,我做了一些简单的事情,但对我来说起作用了。
在我的main.py文件中,也就是你加载QML文件的文件中,我添加了以下内容。
import PyQt5.QtQuick

然后运行pyinstaller:

pyinstaller  -F  - -onefile main.py

它运行成功了


0

对于我来说,在Windows上,问题最终是由于QML2_IMPORT_PATH环境变量未设置所导致的。一旦我将其设置为“C:\Python35\Lib\site-packages\PyQt5\qml”,它就可以正常工作了!


0
使用PyInstaller时,我注意到它在将应用程序冻结时仅会捆绑失败的QML依赖项。你可以通过从Python站点包(<your_python_path>\Lib\site-packages\PyQt5\Qt\qml)中复制QtQuickQtQuick.2文件夹,并将其放置在被冻结的可执行文件旁边来检查是否也是这种情况:
QtQuick
QtQuick.2
your_executable.exe

如果应用程序在此之后正常工作,您可以编辑.spec文件以自动捆绑这些文件夹(pyinstaller在第一次运行时生成.spec文件)。
# -*- mode: python -*-
import os
import site

block_cipher = None

site_packages_dir = site.getsitepackages()[1]
qml_dir = os.path.join(site_packages_dir, 'PyQt5', 'Qt', 'qml')

added_files = [
    (os.path.join(qml_dir, 'QtQuick'), 'QtQuick'),
    (os.path.join(qml_dir, 'QtQuick.2'), 'QtQuick.2'),
]

a = Analysis(['pyqt5_qml.py'],
             binaries=None,
             datas=added_files,
             hiddenimports=[],
             hookspath=[],
             runtime_hooks=[],
             excludes=[],
             win_no_prefer_redirects=False,
             win_private_assemblies=False,
             cipher=block_cipher)
pyz = PYZ(a.pure, a.zipped_data, cipher=block_cipher)

exe = EXE(pyz,
          a.scripts,
          exclude_binaries=True,
          name='app',
          debug=False,
          strip=False,
          upx=False,
          console=True,
)

coll = COLLECT(exe,
               a.binaries,
               a.zipfiles,
               a.datas,
               strip=False,
               upx=False,
               name='pyqt5_qml')

然后尝试运行pyinstaller来处理这个spec文件:pyinstaller pyqt5_qml.spec


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接