Python Forum
Using pyinstaller with .ui GUI files - No such file or directory error - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Using pyinstaller with .ui GUI files - No such file or directory error (/thread-40235.html)



Using pyinstaller with .ui GUI files - No such file or directory error - diver999 - Jun-26-2023

Hi All, I'm sure I'm not the first to encounter this but can't seem to find a solution.I hope someone can help.

I'm using W11, python 3.11 & PyQt5. My python code uses the .ui rather than a compiled .py file & I'm trying to use pyinstaller to make a single .exe. Here's an example which shows the problem. I have seen solutions proposed that need the .ui to be compiled into a.py file. I'd rather not go that route if I can help it. It's a pain to have to compile the .ui every time a change is made & I'll also have to re-write my existing code.

.py code:
from PyQt5 import QtWidgets, uic

class HelloApp(object):
    def __init__(self, app):
        self.app = app
        self.ui = uic.loadUi('helloGUI.ui')
        self.ui.show()
        self.run()
    
    def run(self):
        self.app.exec_()

if __name__ == "__main__":
    import sys
    app = QtWidgets.QApplication(sys.argv)
    HelloApp(app)
... the .ui xml code from PyQt5

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Form</class>
 <widget class="QWidget" name="Form">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>260</width>
    <height>132</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Hello</string>
  </property>
  <layout class="QGridLayout" name="gridLayout">
   <item row="0" column="0">
    <widget class="QLabel" name="label">
     <property name="font">
      <font>
       <family>Arial</family>
       <pointsize>44</pointsize>
       <weight>50</weight>
       <italic>true</italic>
       <bold>false</bold>
       <kerning>true</kerning>
      </font>
     </property>
     <property name="text">
      <string>Hello</string>
     </property>
     <property name="alignment">
      <set>Qt::AlignCenter</set>
     </property>
    </widget>
   </item>
  </layout>
 </widget>
 <resources/>
 <connections/>
</ui>
This should just throw up a simple window with Hello in the middle.

I'm using pyinstaller --onefile --add-data="helloGUI.ui;." HelloApp.py which seems to work, but when I run the .exe in the dist folder, I get a window pop up with:

Traceback (most recent call last):
File "helloAPP.py", line 20, in <module>
File "helloAPP.py", line 10, in __init__
File "PyQt5\uic\__init__.py", line 241, in loadUi
File "PyQt5\uic\Loader\loader.py", line 66, in loadUi
File "PyQt5\uic\uiparser.py", line 1020, in parse
File "xml\etree\ElementTree.py", line 1218, in parse
File "xml\etree\ElementTree.py", line 569, in parse
FileNotFoundError: [Errno 2] No such file or directory: 'helloGUI.ui'

If I add the .ui file to the dist folder, all is OK. I've also tried running the .exe on a machine without python installed & again all is OK if the .ui is in the same folder.

Thanks in advance, Dave


RE: Using pyinstaller with .ui GUI files - No such file or directory error - Axel_Erfurt - Jun-26-2023

On Mac / Linux use :

--add-data="helloGUI.ui:."


RE: Using pyinstaller with .ui GUI files - No such file or directory error - snippsat - Jun-26-2023

Add this,then it should work.
from PyQt5 import QtWidgets, uic
import sys, os

if getattr(sys, 'frozen', False):
    os.chdir(sys._MEIPASS)

class HelloApp(object):
    def __init__(self, app):
        self.app = app
        self.ui = uic.loadUi('helloGUI.ui')
        self.ui.show()
        self.run()

    def run(self):
        self.app.exec_()

if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    HelloApp(app)
In many cases for lager prosje is better to use --onedir,then do not need to fix _MEIPASS(the path that a single .exe will use)
pyinstaller --clean --windowed --onedir --add-data helloGUI.ui;. qt_code.py
Then later can eg pack it with eg Inno Setup then get single setup.exe which has a installer.
Or can just .zip to one file,and share that.
It depend one use case a singe .exe is easy to share a,but many will not a run a executable .exe if not sure what is.
A installer(that also has a uninstaller) also make some Doc,is the professional looking way if want to share in a larger scale.


RE: Using pyinstaller with .ui GUI files - No such file or directory error - diver999 - Jun-27-2023

Thanks snippsat, the additional bit worked fine. I'll certainly look at your further suggestions too.

It's windows btw.