【问题标题】:No such file or directory:'main.ui' in cx_freezecx_freeze 中没有这样的文件或目录:'main.ui'
【发布时间】:2020-12-12 10:24:08
【问题描述】:

我使用 PyQT5 开发了一个使用 python 的软件。 现在我有 main.ui 和 main.py 我用这个命令行来读取 main.ui 文件:

FORM_CLASS,_=loadUiType(path.join(path.dirname(__file__),"main.ui"))

现在我的 main.ui 已连接到编写主要 python 代码的 main.py 文件。

我还按照 cx_freeze 指令创建了 setup.py

然后我使用了cmd命令:

python setup.py build_exe

完成后我收到以下错误:

No such file or directory:'main.ui'

我能解决这个问题吗?

【问题讨论】:

    标签: python pyqt5 executable cx-freeze


    【解决方案1】:

    如果你打算使用外部资源,那么你应该使用data files的cx_freeze手册:

    使用数据文件 应用程序通常除了代码之外还需要数据文件, 比如图标。使用设置脚本,您可以列出数据文件或 build_exe 的 include_files 选项中的目录。他们会 复制到可执行文件旁边的构建目录。然后去寻找 他们,使用这样的代码:

    def find_data_file(filename):
        if getattr(sys, 'frozen', False):
            # The application is frozen
            datadir = os.path.dirname(sys.executable)
        else:
            # The application is not frozen
            # Change this bit to match where you store your data files:
            datadir = os.path.dirname(__file__)
        return os.path.join(datadir, filename)
    

    另一种方法是在代码中嵌入数据,例如使用 Qt 的 资源系统。

    所以在你的情况下修改你的代码:

    import os.path
    import sys
    # ...
    
    def find_data_file(filename):
        if getattr(sys, 'frozen', False):
            # The application is frozen
            datadir = os.path.dirname(sys.executable)
        else:
            # The application is not frozen
            # Change this bit to match where you store your data files:
            datadir = os.path.dirname(__file__)
        return os.path.join(datadir, filename)
    
    # ...
    
    FORM_CLASS,_=loadUiType(find_data_file("main.ui"))
    
    # ...

    并更改 setup.py 以包含 .ui:

    from cx_Freeze import setup, Executable
    
    setup(
        name="mytest",
        version="0.1",
        description="",
        options={"build_exe": {"include_files": "main.ui"}},
        executables=[Executable("main.py")],
    )

    【讨论】:

    • 非常感谢兄弟,它对我有用,使用你的方法,感激..
    猜你喜欢
    • 2019-05-26
    • 1970-01-01
    • 1970-01-01
    • 2021-06-24
    • 1970-01-01
    • 2015-02-20
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多