Python pyinstaller在程序内找不到文件路径
我创建了一个测试工具来检查是否有任何模块不能与pyinstaller一起工作,这样我就可以在主程序上使用pyinstaller之前解决它们 当我尝试与脚本中的文件路径交互时,pyinstaller创建的程序似乎无法找到我尝试硬编码到脚本中的路径,例如“Z:\mkb\crew\mark\u conrad\pictures\psd\u tool\u test\u files\test.psd”。我决定使用一个简单的os.path.exists()来调试这个谜,但运气不好。当我从python控制台运行调试程序时,它工作正常,那么这里出了什么问题 我是如何生成exe的: pyinstaller“Z:\mkb\programming\python\util\pyinstaller\u library\u tester.py” Python版本:2.7.15 PyInstaller版本:3.3.1 领事输出:Python pyinstaller在程序内找不到文件路径,python,python-2.7,pyinstaller,Python,Python 2.7,Pyinstaller,我创建了一个测试工具来检查是否有任何模块不能与pyinstaller一起工作,这样我就可以在主程序上使用pyinstaller之前解决它们 当我尝试与脚本中的文件路径交互时,pyinstaller创建的程序似乎无法找到我尝试硬编码到脚本中的路径,例如“Z:\mkb\crew\mark\u conrad\pictures\psd\u tool\u test\u files\test.psd”。我决定使用一个简单的os.path.exists()来调试这个谜,但运气不好。当我从python控制台运行
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.\u 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编译的程序,它都“正常工作”。请记住,您需要将所有文件添加到spec中,并且它们需要位于相对于代码的路径中,因此,我通常将所需的所有文件都放在项目的主源文件夹或子文件夹中。因此,如果我构建了生成文件夹或txt文件的pyinstall脚本,我需要将这些文件和文件夹的路径添加到使用pyinstaller?MarkC no生成的spec文件中。如果应用程序生成这些文件,只要你知道他们写给你的绝对路径就可以了。这适用于您不知道绝对路径的文件(添加到可执行文件/安装程序的文件),因此需要相对于应用程序解析这些文件binary@MarkC如果这对你有帮助,请考虑投票和/或标记这是公认的答案。