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
  Loop through all files in a directory? Winfried 10 413 Apr-23-2024, 07:38 PM
Last Post: FortuneCoins
  uploading files from a ubuntu local directory to Minio storage container dchilambo 0 470 Dec-22-2023, 07:17 AM
Last Post: dchilambo
  change directory of save of python files akbarza 3 900 Jul-23-2023, 08:30 AM
Last Post: Gribouillis
  Coding error. Can't open directory EddieG 6 1,130 Jul-13-2023, 06:47 PM
Last Post: deanhystad
  Monitoring a Directory for new mkv and mp4 Files lastyle 3 1,654 May-07-2023, 12:33 PM
Last Post: deanhystad
Photo exe created by pyinstaller only works in the dist directory dee 7 2,820 Feb-23-2023, 05:53 PM
Last Post: dee
  Extract file only (without a directory it is in) from ZIPIP tester_V 1 1,009 Jan-23-2023, 04:56 AM
Last Post: deanhystad
Thumbs Up Need to compare the Excel file name with a directory text file. veeran1991 1 1,132 Dec-15-2022, 04:32 PM
Last Post: Larz60+
  no such file or directory in SFTP saisankalpj 2 1,564 Nov-25-2022, 11:07 AM
Last Post: DeaD_EyE
Photo Making Zip file of a file and Directory Nasir 2 1,037 Oct-07-2022, 02:01 PM
Last Post: Nasir

Forum Jump:

User Panel Messages

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