PyInstaller + UI 文件 - FileNotFoundError: [Errno 2] 没有这样的文件或目录: [英] PyInstaller + UI Files - FileNotFoundError: [Errno 2] No such file or directory:

查看:54
本文介绍了PyInstaller + UI 文件 - FileNotFoundError: [Errno 2] 没有这样的文件或目录:的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 PyInstaller 将我的 .py 脚本导出到 .exe,它依赖于使用 Qt 设计器创建的 .ui 文件.

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

我可以确认我的 .py 脚本在通过 PyCharm 运行时运行良好 - 我能够看到我使用 .ui 文件创建的 GUI.

但是,当我将 .py 脚本导出到 .exe 并启动它时,我在命令行中收到以下错误:

However, when I export my .py script to .exe and launch it, I recieve the following errors in 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

出于某种原因,.exe 文件正在寻找路径中的 .ui 文件 - C:\Users\giranm\

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

然而,已经做了一些研究,我被告知我需要使用 os.getcwd() 并确保我的脚本中有完整路径.即使使用下面的代码,我仍然会在尝试定位 .ui 文件时遇到错误.

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] 没有那个文件或目录:

# 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.  

我知道可以将 .ui 文件转换为 .py 并使用 pyuic4 将它们导入到主程序中.但是,我将对 .ui 文件进行多次编辑因此,我无法继续转换它们.

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.

有没有办法解决这个问题,以便我可以创建一个独立的 .exe?

我刚开始使用 PyQT4 和 PyInstaller - 任何帮助将不胜感激!

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

推荐答案

在整个周末都在挠头并进一步研究 SO 之后,我设法使用 UI 文件按预期编译了独立的 .exe.

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

使用 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)

接下来,我使用此函数和所需类的变量导入了 .UI 文件.

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)

然后我不得不使用 Qt Designer 创建一个资源文件 (.qrc) 并使用这个资源文件嵌入图像/图标.完成后,我使用 pyrcc4 将 .qrc 文件转换为 .py 文件,该文件将被导入到主脚本中.

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.

终端

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

Python

import resources_rc

确认主 .py 脚本有效后,我使用 PyInstaller 创建了一个 .spec 文件.

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

终端

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

根据 PyInstaller 的指南,我通过修改上述 .spec 文件添加了数据文件.

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

最后,我使用上面的 .spec 文件编译了 .exe.

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

这篇关于PyInstaller + UI 文件 - FileNotFoundError: [Errno 2] 没有这样的文件或目录:的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆