【发布时间】:2016-08-10 23:47:47
【问题描述】:
我正在使用 pyinstaller 编译一个 python 应用程序。
结构是这样的——
d:\app\myprog.exe
d:\app\config\settings.conf
如果我从d:\app 运行myprog.exe --switch value,它运行良好,如果我尝试从c:\windows 等其他任何地方运行,它就找不到我的settings.conf 文件抱怨消息:
Traceback (most recent call last):
File "<string>", line 284, in <module>
File "<string>", line 218, in main
File "ConfigParser.py", line 330, in get
ConfigParser.NoSectionError: No section: 'database'
myApp returned -1
database 是我尝试引用的配置文件中的第一行。
我在这里从应用程序中引用BASE_DIR -
# Global Path and Config Info
try:
BASE_DIR = os.path.dirname(os.path.abspath(__file__))
except NameError: # We are the main py2exe script, not a module
BASE_DIR = os.path.dirname(os.path.abspath(sys.argv[0]))
这样我就可以得到这样的配置文件 -
config = RawConfigParser()
config.read(os.path.join(BASE_DIR, 'config/settings.conf'))
但我猜BASE_DIR 是我从中运行 EXE 的任何文件夹(例如 c:\windows,而不是 EXE 的位置(即 d:\app)?
【问题讨论】:
-
我认为这减少了this question 的重复。另请参阅 pyinstaller 和 cx_Freeze 文档。
标签: python python-2.7 pyinstaller