pyinstaller 在程序中找不到文件路径

pyinstaller not finding file path inside program

我创建了一个测试工具来检查是否有任何模块不能与 pyinstaller 一起工作,这样我就可以在我的主程序上使用 pyinstaller 之前解决它们。

当我尝试与我的脚本中的文件路径交互时,pyinstaller 创建的程序似乎无法找到我尝试硬编码到脚本中的路径,例如 "Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd"。我决定使用简单的 os.path.exists() 来调试这个谜团,但没有运气。当我从 python 控制台 运行 我的调试程序时,它工作正常所以这里出了什么问题?

我是如何生成 exe 的: pyinstaller "Z:\mkb\programing\python\util\pyinstaller_library_tester.py"

Python版本:2.7.15 PyInstaller 版本:3.3.1

领事输出:

Testing: Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd
>>> This path does not exsist.
Path Results: False

Testing: Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd
>>> This path does not exsist.
Path Results: False

Testing: Z:/mkb/crew/mark_conrad/pictures/psd_tool_test_files/test.psd
>>> This path does not exsist.
Path Results: False

Testing: Z://mkb//crew//mark_conrad//pictures//psd_tool_test_files//test.psd
>>> This path does not exsist.
Path Results: False

调试程序代码:

def checkingPaths(path,btn):
    import os

    if os.path.exists(path):
        print '>>> Found a working path use this for your formats for paths'
        print 'Path Results:',os.path.exists(path)
        btn.configure(bg='#00cc30')
    else:
        print '>>> This path does not exsist.'
        print 'Path Results:',os.path.exists(path)
        btn.configure(bg='#ff0000')

def osTest(btn):

    print r'Testing: Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd'
    checkingPaths("Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd",btn)

    print r'Testing: Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd'
    checkingPaths("Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd",btn)

    print r'Testing: Z:/mkb/crew/mark_conrad/pictures/psd_tool_test_files/test.psd'
    checkingPaths("Z:/mkb/crew/mark_conrad/pictures/psd_tool_test_files/test.psd",btn)

    print r'Testing: Z://mkb//crew//mark_conrad//pictures//psd_tool_test_files//test.psd'
    checkingPaths("Z://mkb//crew//mark_conrad//pictures//psd_tool_test_files//test.psd",btn)

def tkinterTest():
    import Tkinter as tk

    root = tk.Tk()

    osBtn = tk.Button(root,text='os Test',command =lambda: osTest(osBtn))

    osBtn.pack(padx=10,pady=2,fill='x')

    root.mainloop()

tkinterTest()

它创建了一个新路径,当 "compiled" 在 sys._MEIPASS 下时,您必须使用它。我通常会创建一个函数,根据 运行 在 python 或 "compiled" 时解析相对资源路径,如下所示:

def get_correct_path(relative_path):
    try:
        base_path = sys._MEIPASS
    except Exception:
        base_path = os.path.abspath(".")

    return os.path.join(base_path, relative_path)

还要确保将这些文件正确地包含在规范文件中。