4
votes

I'm trying to build an app using pyinstaller, PyQt5 and qml (see files below) using the following command.

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)

The pyqt5_qml.py runs fine (open an "Hello world!" window) but the built app complains about module "QtQuick" version 2.4 is not installed. I guess the module has not been included into the built app but I'm not sure how to tell pyinstaller to do it.

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
Did you ever get anywhere with this? - egfconnor
No, not event a hint... - Nicolas Rougier
I'm facing the same issue. I've tried Linux and Windows and am new to QT, Python, and PyQT in general so it's a bit rough. - egfconnor

3 Answers

1
votes

I hope this could help

I was struggling with the same problem

After hours, I did something simple, but for me it worked

In my main.py I mean the file in which you load the QML file I added

import PyQt5.QtQuick

And then run pyinstaller :

pyinstaller  -F  - -onefile main.py

And it worked

0
votes

For me it on Windows this ended up being the QML2_IMPORT_PATH environment variable had not been set. Once I set this to "C:\Python35\Lib\site-packages\PyQt5\qml" it worked!

0
votes

Working with PyInstaller I've noticed that it simply fails to bundle QML dependencies then freezing an app. You can check whether it is your case as well by copying QtQuick and QtQuick.2 folders from python site packages (<your_python_path>\Lib\site-packages\PyQt5\Qt\qml) and placing it near freezed executable:

QtQuick
QtQuick.2
your_executable.exe

If app works after this, you can edit .spec file to bundle these folders automatically (pyinstaller generate .spec-file on first run).

# -*- 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')

Then try to run pyinstaller against this spec-file: pyinstaller pyqt5_qml.spec