【问题标题】:pyinstaller not finding file path inside programpyinstaller 在程序中找不到文件路径
【发布时间】:2018-05-12 01:57:29
【问题描述】:

我创建了一个测试工具来检查是否有任何模块不能与 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()

【问题讨论】:

    标签: python python-2.7 pyinstaller


    【解决方案1】:

    它会创建一个新路径,您必须在 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)
    

    还要确保在规范文件中正确包含文件。

    【讨论】:

    • 谢谢你我是新来的 pyinstall。这是我需要添加到调试程序代码中的函数吗?每当我通过函数调用路径时,我需要通过 get_correct_path 函数运行它以获得正确的路径?
    • @MarkC 这就是我使用它的方式,所以我不必尝试/除非每次我需要路径时都这样做。这样,无论是在 dev 中的 python 中运行还是在 prod 中通过 pyinstaller 编译运行,它都“正常工作”。请记住,您需要将所有文件添加到规范中,并且它们需要位于相对于代码的路径中,因此我通常将所需的所有文件放在主源文件夹或项目中的子文件夹中
    • 因此,如果我构建了一个生成文件夹或 txt 文件的 pyinstall 脚本,我需要将这些文件和文件夹的路径添加到使用 pyinstaller 生成的规范文件中?
    • @MarkC 没有。如果应用程序生成文件,只要您知道它们写入的绝对路径就可以了。这适用于您不知道绝对路径的文件(添加到可执行文件/安装程序的文件),因此需要相对于应用程序二进制文件解析它们
    • @MarkC 如果这对您有帮助,请考虑投票和/或将其标记为已接受的答案。
    猜你喜欢
    • 1970-01-01
    • 2021-10-18
    • 2018-05-09
    • 2012-09-18
    • 1970-01-01
    • 1970-01-01
    • 2021-12-01
    • 1970-01-01
    相关资源
    最近更新 更多