Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. Qt for Python
  4. Qt for Python & PyInstaller Fails
Forum Updated to NodeBB v4.3 + New Features

Qt for Python & PyInstaller Fails

Scheduled Pinned Locked Moved Unsolved Qt for Python
4 Posts 2 Posters 763 Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    raga
    wrote on 31 May 2021, 13:38 last edited by
    #1

    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.

    N 1 Reply Last reply 1 Jun 2021, 08:46
    0
    • R raga
      31 May 2021, 13:38

      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.

      N Offline
      N Offline
      ndias
      wrote on 1 Jun 2021, 08:46 last edited by
      #2

      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

      1 Reply Last reply
      0
      • R Offline
        R Offline
        raga
        wrote on 1 Jun 2021, 12:26 last edited by
        #3

        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_())
        
        N 1 Reply Last reply 1 Jun 2021, 13:10
        0
        • R raga
          1 Jun 2021, 12:26

          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_())
          
          N Offline
          N Offline
          ndias
          wrote on 1 Jun 2021, 13:10 last edited by
          #4

          @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

          1 Reply Last reply
          0

          1/4

          31 May 2021, 13:38

          • Login

          • Login or register to search.
          1 out of 4
          • First post
            1/4
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved