Search code examples
qmlexecx-freezepyqt5

PyQt5 executable application with qml


I'm trying to build a simple executable PyQt5 application by cx_Freeze, but when i start the builded exe file, it tells me an error, can't find qml file

main.py

from PyQt5.QtNetwork import *
from PyQt5.QtQml import *
from PyQt5.QtWidgets import *
from PyQt5.QtCore import *

class MainWin(object):
    def __init__(self):
        self.eng = QQmlApplicationEngine()
        self.eng.load('main.qml')
        win = self.eng.rootObjects()[0]
        win.show()

if __name__ == '__main__':
    import sys
    App = QApplication(sys.argv)
    Win = MainWin()
    sys.exit(App.exec_())

main.qml

import QtQuick 2.2
import QtQuick.Controls 1.0
ApplicationWindow {
    id: main
    width: 400
    height: 600
    color: 'grey'
 }

Solution

  • I've found an answer, i should to include qtquick in cx_freeze setup.py, something like this:

    if sys.platform == "win32":
        base = "Win32GUI"
        PYQT5_DIR = "d:/programs/Python3/lib/site-packages/PyQt5"
        include_files = [
            "qml/",
            (os.path.join(PYQT5_DIR, "qml", "QtQuick.2"), "QtQuick.2"),
            (os.path.join(PYQT5_DIR, "qml", "QtQuick"), "QtQuick"),
            (os.path.join(PYQT5_DIR, "qml", "QtGraphicalEffects"), "QtGraphicalEffects"),
        ]
    
    setup(
        name="exe",
        version="0.9",
        description="asd",
        author="beast",
        author_email="[email protected]",
        options={"build_exe": {"includes": ["atexit",     "sip","PyQt5.QtCore","PyQt5.QtGui","PyQt5.QtWidgets",
                                        "PyQt5.QtNetwork","PyQt5.QtOpenGL", "PyQt5.QtQml", "PyQt5.QtQuick"],
                           "include_files": include_files,
                           "excludes" : ['Tkinter'],
                           # "optimize" : 2,
                           # "compressed" : True,
                           # "include_msvcr" : True,
                       }},
    executables=[
        Executable(script="main.py",
                   targetName="EVTicket.exe",
                   base=base)
    ]
    )