PyInstaller + UI files - FileNotFoundError: [Errno 2] There is no such file or directory:

I am trying to export my .py script to .exe using PyInstaller, which has dependencies on .ui files that were created using Qt Designer.

I can confirm that my .py script works fine when run through PyCharm - I can see the GUI that I created with the .ui files.

However, when I export my .py script to .exe and run it, I get the following errors on the command line:

C:\Users\giranm>"C:\Users\giranm\PycharmProjects\PyQt Tutorial\dist\secSearch_demo.exe" Traceback (most recent call last): File "secSearch_demo.py", line 13, in <module> File "site-packages\PyQt4\uic\__init__.py", line 208, in loadUiType File "site-packages\PyQt4\uic\Compiler\compiler.py", line 140, in compileUi File "site-packages\PyQt4\uic\uiparser.py", line 974, in parse File "xml\etree\ElementTree.py", line 1186, in parse File "xml\etree\ElementTree.py", line 587, in parse FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\giranm\\securitySearchForm.ui' Failed to execute script secSearch_demo 

For some reason, the .exe file is looking for the .ui file in the path - C: \ Users \ giranm \

However, after doing some research, I was told that I need to use os.getcwd () and make sure that I have the full path in my script. Even with the code below, I still get errors trying to find .ui files.

PyInstaller: IOError: [Errno 2] There is no such file or directory:

 # import relevant modules etc... cwd = os.getcwd() securitySearchForm = os.path.join(cwd, "securitySearchForm.ui") popboxForm = os.path.join(cwd, "popbox.ui") Ui_MainWindow, QtBaseClass = uic.loadUiType(securitySearchForm) Ui_PopBox, QtSubClass = uic.loadUiType(popboxForm) # remainder of code below. 

I know that you can convert .ui files to .py and import them into the main procedure using pyuic4. However, I will make several changes to the .ui files and therefore it is not possible for me to convert them.

Is there a way to fix this so that I can create a standalone .exe?

I'm new to using PyQT4 and PyInstaller - any help would be greatly appreciated!

+7
python pyinstaller pyqt4 qt-designer
source share
2 answers

After I scratched my head all the time and looked beyond SO, I managed to compile a standalone .exe, as expected, using the user interface files.

First, I defined the following function using this answer

Merging data files using PyInstaller (--onefile)

 # Define function to import external files when using PyInstaller. def resource_path(relative_path): """ Get absolute path to resource, works for dev and for PyInstaller """ try: # PyInstaller creates a temp folder and stores path in _MEIPASS base_path = sys._MEIPASS except Exception: base_path = os.path.abspath(".") return os.path.join(base_path, relative_path) 

Then I imported the .UI files using this function and the variables for the required classes.

 # Import .ui forms for the GUI using function resource_path() securitySearchForm = resource_path("securitySearchForm.ui") popboxForm = resource_path("popbox.ui") Ui_MainWindow, QtBaseClass = uic.loadUiType(securitySearchForm) Ui_PopBox, QtSubClass = uic.loadUiType(popboxForm) 

Then I had to create a resource file (.qrc) using Qt Designer and insert images / icons using this resource file. After that, I used pyrcc4 to convert the .qrc file to a .py file, which will be imported into the main script.

Terminal

 C:\Users\giranm\PycharmProjects\PyQt Tutorial>pyrcc4 -py3 resources.qrc -o resources_rc.py 

Python

 import resources_rc 

Once I confirmed that the main .py script is working, I created the .spec file using PyInstaller.

Terminal

 C:\Users\giranm\PycharmProjects\PyQt Tutorial>pyi-makespec --noconsole --onefile secSearch_demo.py 

According to the PyInstaller manual, I added data files by modifying the above .spec file.

https://pythonhosted.org/PyInstaller/spec-files.html#adding-data-files

Finally, I then compiled .exe using the .spec file on top.

+7
source share

You can simply use:

 uic.loadUi('E:\Development\Python\your_ui.ui', self) 

Use the full path and use pyinstaller with standard arguments and it works fine.

0
source share

All Articles