Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt for Python & PyInstaller Fails



  • Hello,
    I am trying to deploy my Qt Quick application. I followed these instructions. After I run the application I get this error message.

    Gtk-Message: 16:37:21.429: Failed to load module "canberra-gtk-module"
    Gtk-Message: 16:37:21.430: Failed to load module "canberra-gtk-module"
    QQmlApplicationEngine failed to load component
    file:///tmp/_MEI7lG9R2/main.qml: No such file or directory
    

    What am I doing wrong?
    Thanks.



  • Hi @raga,

    When -onefile option is used, pyinstaller unpacks data into a temporary folder. This directory path is stored in the _MEIPASS2 environment variable.

    Bellow I provide you a method you can get the _MEIPASS2 directory. This way your resource files should be obtained from:

    def resource_path(relative_path):
        try:
            # PyInstaller creates a temp folder and stores path in _MEIPASS
            base_path = sys._MEIPASS
        except Exception:
             base_path = os.path.dirname(__file__)
        return  os.path.join(base_path, relative_path)
    
        ...
        engine = QQmlApplicationEngine(resource_path('main.qml'))
        appIcon = QIcon(resource_path('myIcon.ico'))
    

    Please, don't forget to include resource files in the pyinstaller command. Example:

    pyinstaller --add-data = "main.qml;." --add-data = "myIcon.ico;." myApp.py
    

    You can call --add-data multiple times.

    If you are not using the -onefile option or if it has not solved your problem please provide more details.

    Regards



  • Dear @ndias,
    Thanks for you response.
    I didn't know that we have to add each qml file while freezing the application as not mentioned here. After your suggestions. I used the lines below without adding the resource_path function.

    pyinstaller --add-data "main.qml:." --add-data "Section_output.qml:." --add-data "Window_func.qml:." --add-data "icon.webp:." --onefile main.py
    

    The application is running well. However, the icon not appears and still getting the Gtk messages.
    The main.py is as below.

    # This Python file uses the following encoding: utf-8
    import sys
    import os
    
    from PySide2.QtGui import QIcon
    from PySide2.QtQml import QQmlApplicationEngine
    from PySide2.QtWidgets import QApplication
    
    import pyDAQ
    
    if __name__ == "__main__":
        app = QApplication(sys.argv)
        app.setWindowIcon(QIcon("icon.webp"))
        engine = QQmlApplicationEngine()
    
        daq = pyDAQ.pyDAQ()
        engine.rootContext().setContextProperty("backend", daq)
    
        engine.load(os.path.join(os.path.dirname(__file__), "main.qml"))
    
        if not engine.rootObjects():
            sys.exit(-1)
        sys.exit(app.exec_())
    


  • @raga:

    If you will use --onefile option you will probably need to add resource_path() function or other similar solution. As a first iteration, I recommend that you do not use this option. Then, check if you are able to run your application and if your .qml and .webp files are contained on output folder dist.

    Regarding your pyinstaller instruction you need to use ';' on --add-data parameters. You are using ':'.

    At last, note to set your application icon you should also include --icon=icon.webp option on pyinstaller instruction.

    Regards


Log in to reply