0

I'm trying to generate an executable with pyinstaller. I'm using PyQt5 so I have an .ui-File. I need an executable, where the ui file is not needed separately so that everything is in one file. I tried several things, e.g like described here but I didn't get it. Can anyone help me? I get the error FileNotFoundError: [Errno 2] No such file or directory: 'C:\Users\xxx\AppData\Local\Temp\_MEI181562\visu.ui'

Here is a minimal example: visu.py

import sys
from PyQt5 import QtWidgets, uic
Ui_MainWindow, QtBaseClass = uic.loadUiType('visu.ui')
class MyWindow(QtWidgets.QMainWindow, Ui_MainWindow):
    def __init__(self):
        QtWidgets.QMainWindow.__init__(self)
        Ui_MainWindow.__init__(self)
        self.setupUi(self)
if __name__ == "__main__":
    app = QtWidgets.QApplication(sys.argv)
    window = MyWindow()
    window.show()
    sys.exit(app.exec_())

And visu.ui

<?xml version="1.0" encoding="UTF-8"?>
<ui version="4.0">
 <class>Dialog</class>
 <widget class="QDialog" name="Dialog">
  <property name="geometry">
   <rect>
    <x>0</x>
    <y>0</y>
    <width>172</width>
    <height>122</height>
   </rect>
  </property>
  <property name="windowTitle">
   <string>Dialog</string>
  </property>
  <widget class="QPushButton" name="pushButton">
   <property name="geometry">
    <rect>
     <x>40</x>
     <y>50</y>
     <width>93</width>
     <height>28</height>
    </rect>
   </property>
   <property name="text">
    <string>Ok</string>
   </property>
  </widget>
 </widget>
 <resources/>
 <connections/>
</ui>
csi
  • 230
  • 1
  • 7
  • 22
  • I don't understand. You say that the "the ui file is not needed separately". But, apparently, you need it to build your interface (which means that it's always needed, even in the frozen build). How and where do you use `QtBaseClass`? Please provide a [minimal, reproducible example](https://stackoverflow.com/help/minimal-reproducible-example) of your code. – musicamante Mar 03 '20 at 14:22
  • @musicamante For building the python script, the ui file is needed. But I need an executable to give to other people and this should be one executable without extra ui file – csi Mar 03 '20 at 14:25
  • @csi If you use an ui file, you will need that even for the bundled version, so you'll have to create a single file based binary. Check the [what to generate](https://pythonhosted.org/PyInstaller/usage.html#what-to-generate) section in the documentation. Also, does this help? https://stackoverflow.com/questions/13946650/pyinstaller-2-0-bundle-file-as-onefile – musicamante Mar 03 '20 at 14:33
  • @MauriceMeyer I read this post as well, but I didn't get it what I have to do exactly. I get the same error like before (FileNotFoundError) – csi Mar 03 '20 at 14:33
  • @musicamante I already use --onefile, but the ui file is not included, it still has to be stored in the same folder as the executable – csi Mar 03 '20 at 14:40
  • @csi you have to follow the instructions about including files in the bundle (specifically, adding external files to the `.spec` file). – musicamante Mar 03 '20 at 15:01
  • I tried to change the spec file by changing datas to datas=[ ('path/to/visu.ui', '.') ] and start pyinstaller visu.spec, but it doesn't work... – csi Mar 03 '20 at 15:10
  • Please, be more specific. What doesn't work? It doesn't build or it does? Is the file bundled (even if it doesn't work, it might be an issue related to the path)? If you try to use the --onedir mode does it work? Check the output log of pyinstaller by using --log-level DEBUG. – musicamante Mar 03 '20 at 15:50
  • @csi remove the `if` statement and all references of `bundle_dir` and `guipath`. Then change the load statement to `uic.loadUiType('visu.ui')`. Then build with the data command line option; `--add-data="visu.ui;."`. Please refer to the docs for info. – Legorooj Mar 03 '20 at 23:40
  • @csi that will work in both `--onefile` and `--onedir` mode. – Legorooj Mar 03 '20 at 23:46
  • @musicamante the build works, but I always get the error, that the ui file could not be found, when I try to start the exe file – csi Mar 04 '20 at 07:50
  • @Legorooj When I remove the if statement etc, I have the same code than I already wrote at the top, right? Ui_MainWindow, QtBaseClass = uic.loadUiType('visu.ui') . Then I start pyinstaller.exe --onefile D:/visu.py --add-data="D:/visu.ui;." When I start the exe file, I get the error again "No such file or directory: "visu.ui" – csi Mar 04 '20 at 07:55
  • @musicamante onedir is working, then it saves the ui file in the same folder. But I need one single exe file.. – csi Mar 04 '20 at 08:01
  • @csi According to [adding data files](https://pyinstaller.readthedocs.io/en/stable/spec-files.html#adding-data-files), you have to use a colon to specify the target path, not a semicolon. – musicamante Mar 04 '20 at 08:19
  • @musicamante I tried this as well, but then I cannot build and get the error pyinstaller: error: argument --add-data: invalid add_data_or_binary value: 'D:/visu.ui:.' – csi Mar 04 '20 at 08:26
  • 1
    I see now that on Windows the semicolon is to be used, so, that was fine. But have you tried to use the backslash for the path? Or, simply run pyinstaller from the directory in which the files reside and use the file names only, without any source path specification (meaning, except for the "." target, obviously): `pyinstaller.exe --onefile --add-data="visu.ui;." visu.py` – musicamante Mar 04 '20 at 08:36
  • @musicamante I did not try this before and copied the files to the same folder, but it's not working either :( – csi Mar 04 '20 at 08:50
  • @csi build in onedir and remove the if block as descibed before. Then copy the `visu.ui` file into the `dist/visu` directory. Does that work? – Legorooj Mar 04 '20 at 08:55
  • @Legorooj when I build in onedir, the visu.ui is copied automatically to dist/visu by pyinstaller. This works, but I need a single exe file – csi Mar 04 '20 at 08:59
  • I added a minimal example – csi Mar 04 '20 at 09:05
  • Finally I fixed it. The only problem was that I used a relative path... Using the absolute path fixed the problem – csi Mar 11 '20 at 08:20
  • @csi So now are you able to make single .exe with embed .ui files?? – Siddheshwar Soni Jan 18 '23 at 12:41
  • @SiddheshwarSoni actually I cannot remember in detail as it is three years ago and I did need it only once. But as I wrote in my comment it looks like the absolute path has solved my problem – csi Jan 20 '23 at 18:22

0 Answers0