PyInstaller + PyQt5 + QML:未安装QtQuick

时间:2015-11-27 21:50:41

标签: python qml pyinstaller pyqt5

我正在尝试使用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 个答案:

答案 0 :(得分:0)

对我而言,在Windows上,这最终导致QML2_IMPORT_PATH环境变量尚未设置。一旦我将其设置为&#34; C:\ Python35 \ Lib \ site-packages \ PyQt5 \ qml&#34;它有效!

答案 1 :(得分:0)

使用PyInstaller我注意到它无法捆绑QML依赖项,然后冻结应用程序。您可以通过从python站点包(QtQuick)复制QtQuick.2<your_python_path>\Lib\site-packages\PyQt5\Qt\qml文件夹并将其放在freezed executable附近来检查是否是您的情况:

QtQuick
QtQuick.2
your_executable.exe

如果应用程序在此之后工作,您可以编辑.spec文件以自动捆绑这些文件夹(pyinstaller generate .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')

然后尝试针对此规范文件运行pyinstallerpyinstaller pyqt5_qml.spec

答案 2 :(得分:0)

我希望这可以帮助

我正在努力解决同样的问题

几个小时后,我做了一些简单的事情,但对我而言,它起作用了

在我的 main.py 中,我指的是您加载我添加的QML文件的文件

select * from `user` where `profile`->"$.year" IN ("2001", "1")

然后运行pyinstaller:

import PyQt5.QtQuick

它有效