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

1 python pyinstaller python-2.7

我创建了一个测试工具来检查是否有任何模块无法与 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
Run Code Online (Sandbox Code Playgroud)

调试程序代码:

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()
Run Code Online (Sandbox Code Playgroud)

Art*_*ent 6

它创建了一个新路径,您在sys._MEIPASS. 我通常创建一个函数来解析相对资源路径,具体取决于是否在 python 中运行或“编译”时运行,如下所示:

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)
Run Code Online (Sandbox Code Playgroud)

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