首页 > 解决方案 > 在 Pyinstaller App 的子目录中包含并启动 py 文件

问题描述

(Mac,python 3.9,pyinstaller)为了组织,我将一些 .py 文件放在我的主要 python 代码的子目录中。这些 .py 文件是从上层目录中的另一个脚本调用的,它们会在子目录中创建一些 csv 文件。

/Main/GUI.py <--调用 /Sub/CreateCSV.py /Main/Sub/CreateCSV.py

我试图使用 pyinstaller 创建一个可以让同事运行的应用程序。我在链接子目录中的 .py 文件时遇到问题。

我在 Analysis 结构的 .spec 文件中添加了一个数据调用:

datas=[ ('Sub/*', 'Sub')],

pyinstaller 将毫无错误地运行,我在 Main/dist/main/ 文件夹中看到 /sub 文件。我已经打印了脚本的运行目录,它不是从 Main/dist 运行的,它的运行位置是:/var/folders/sk/55lnhvws0qz5rsddrtjln8lw0000gn/T/_MEIeOEezI

这就是我在子目录中调用 .py 文件的方式:

def execfile(filepath, globals=None, locals=None):
 Function for executing another python file 
    if globals is None:
        globals = {}
    globals.update({
        "__file__": filepath,
        "__name__": "__main__",
    })
    with open(filepath, 'rb') as file:
        exec(compile(file.read(), filepath, 'exec'), globals, locals)

     if not os.path.exists('./Sub/random.csv'):
            execfile("/Sub/CreateCSV.py)

终端输出给了我错误代码: FileNotFoundError: [Errno 2] No such file or directory: '/Sub/CreateCSV.py'

有什么建议么?

标签: pythonpyinstaller

解决方案


推荐阅读