我在PyInstaller和Py2exe上遇到了同样的问题,所以我遇到了来自cx-freeze的FAQ上的解决方案。
从控制台或作为应用程序使用脚本时,以下功能将为您提供“执行路径”,而不是“实际文件路径”:
print(os.getcwd())
print(sys.argv[0])
print(os.path.dirname(os.path.realpath('__file__')))
资料来源:http :
//cx-freeze.readthedocs.org/en/latest/faq.html
您的旧行(最初的问题):
def read(*rnames):
return open(os.path.join(os.path.dirname(__file__), *rnames)).read()
使用以下代码段替换您的代码行。
def find_data_file(filename):
if getattr(sys, 'frozen', False):
# The application is frozen
datadir = os.path.dirname(sys.executable)
else:
# The application is not frozen
# Change this bit to match where you store your data files:
datadir = os.path.dirname(__file__)
return os.path.join(datadir, filename)
使用上面的代码,您可以将应用程序添加到os的路径中,可以在任何地方执行它,而不会出现应用程序无法找到其数据/配置文件的问题。
用python测试过:
3.3.4
2.7.13
holdtom
相关分类