Python Forum
Using pyinstaller with .ui GUI files - No such file or directory error
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Using pyinstaller with .ui GUI files - No such file or directory error
#1
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
Reply
#2
On Mac / Linux use :

--add-data="helloGUI.ui:."
Reply
#3
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.
Reply
#4
Thanks snippsat, the additional bit worked fine. I'll certainly look at your further suggestions too.

It's windows btw.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Trying to use pyinstaller results in a syntax error mark1969 2 57 Apr-15-2026, 01:59 PM
Last Post: snippsat
  why: pyinstaller generates a different exe file every time momo1313 1 74 Jan-23-2026, 03:17 PM
Last Post: DeaD_EyE
  subprocess FileNotFoundError: [Errno 2] No such file or directory: Pedroski55 11 2,555 Aug-07-2025, 02:10 AM
Last Post: Pedroski55
  Errors during run .exe file generated with Pyinstaller BushyAxis793 4 5,609 Mar-25-2025, 05:57 PM
Last Post: BushyAxis793
  deleting files in program files directory RRADC 6 6,649 Aug-21-2024, 06:11 PM
Last Post: snippsat
  FileNotFoundError: [Errno 2] No such file or directory although the file exists Arnibandyo 0 2,846 Aug-12-2024, 09:11 AM
Last Post: Arnibandyo
  Executable file compiled by PyInstaller does not work on Windows 7 amusaber 1 4,583 Jul-11-2024, 02:59 PM
Last Post: DeaD_EyE
  "[Errno 2] No such file or directory" (.py file) IbrahimBennani 13 14,740 Jun-17-2024, 12:26 AM
Last Post: AdamHensley
  Filer and sort files by modification time in a directory tester_V 5 4,100 May-02-2024, 05:39 PM
Last Post: tester_V
  Loop through all files in a directory? Winfried 10 8,669 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020