pyinstaller 在程序中找不到文件路径 [英] pyinstaller not finding file path inside program

查看:167
本文介绍了pyinstaller 在程序中找不到文件路径的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I created a test tool to check if there are any modules that do not work with pyinstaller so I can work them out before using pyinstaller on my main program.

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

When I try to interact with files paths in my script it looks like the program that pyinstaller created can not find the paths I have tried to hard code into the script such as "Z:\mkb\crew\mark_conrad\pictures\psd_tool_test_files\test.psd". I decided to use a simply os.path.exists() to debug this mystery but with no luck. When I run my debug program from python console it works just fine so what is going wrong here?

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

How I generated the exe: pyinstaller "Z:\mkb\programing\python\util\pyinstaller_library_tester.py"

Python 版本:2.7.15PyInstaller 版本:3.3.1

Python version: 2.7.15 PyInstaller version: 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()

推荐答案

它会在 sys._MEIPASS 下创建编译"时必须使用的新路径.我通常会创建一个函数来解析相对资源路径,具体取决于是在 python 中运行还是在编译"时,如下所示:

It creates a new path you have to use when "compiled" under sys._MEIPASS. I generally make a function that resolves the relative resource path depending on whether running in python or when "compiled", like so:

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)

还要确保在规范文件中正确包含这些文件.

Also ensure you properly include the files in your spec file.

这篇关于pyinstaller 在程序中找不到文件路径的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆