Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
997 views
in Technique[技术] by (71.8m points)

python - PyInstaller + UI Files - FileNotFoundError: [Errno 2] No such file or directory:

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

I can confirm that my .py script works just fine when running it through PyCharm - I'm able to see the GUI I've created with the .ui files.

However, when I export my .py script to .exe and launch it, I recieve the following errors in the command line:

C:Usersgiranm>"C:UsersgiranmPycharmProjectsPyQt TutorialdistsecSearch_demo.exe"
Traceback (most recent call last):
  File "secSearch_demo.py", line 13, in <module>
  File "site-packagesPyQt4uic\__init__.py", line 208, in loadUiType
  File "site-packagesPyQt4uicCompilercompiler.py", line 140, in compileUi
  File "site-packagesPyQt4uicuiparser.py", line 974, in parse
  File "xmletreeElementTree.py", line 1186, in parse
  File "xmletreeElementTree.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 within the path - C:Usersgiranm

However, having done some research already, I was told that I needed to use os.getcwd() and ensure that I have the full path in my script. Even with the code below, I still get errors trying to locate the .ui files.

PyInstaller: IOError: [Errno 2] 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'm aware that one can convert .ui files to .py and import them into the main routine using pyuic4. However, I will be making multiple edits to the .ui files and thus it is not feasible for me to keep converting them.

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

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

After scratching my head all weekend and looking further on SO, I managed to compile the standalone .exe as expected using the UI files.

Firstly, I defined the following function using this answer

Bundling data files with 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)

Next I imported the .UI files using this function and 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)

I then had to create a resource file (.qrc) using Qt Designer and embed images/icons using this resource file. Once done, I used pyrcc4 to convert the .qrc file to .py file, which would be imported in the main script.

Terminal

C:UsersgiranmPycharmProjectsPyQt Tutorial>pyrcc4 -py3 resources.qrc -o resources_rc.py

Python

import resources_rc

Once I have confirmed the main .py script works, I then created a .spec file using PyInstaller.

Terminal

C:UsersgiranmPycharmProjectsPyQt Tutorial>pyi-makespec --noconsole --onefile secSearch_demo.py

As per PyInstaller's guide, I've added data files by modifying the above .spec file.

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

Finally, I then compiled the .exe using the .spec file from above.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...