pyinstaller:无法加载配置文件

时间:2017-08-15 16:40:47

标签: python pyinstaller

我有一个非常基本的烧瓶应用程序,可以导入配置文件,如下所示:

app.config.from_pyfile('config.py')

我正在尝试使用PyInstaller捆绑此烧瓶应用程序,PyInstaller会分析所有导入以创建其依赖项列表。由于config.py未明确导入,我相信我需要将其添加为附加数据文件。每this SO question我尝试过以下内容:

pyinstaller --onefile --add-data "config.py:config.py"

但是,当我运行捆绑的可执行文件时,我收到以下错误:

FileNotFoundError: [Errno 2] Unable to load configuration file (No such file or directory): 'Users/Johnny/config.py'

当我显式导入配置文件时,一切正常:

from config import *

但我想弄清楚为什么我的初始方法不起作用。注意我正在运行macOS 10.12.5和python 3.4.5

1 个答案:

答案 0 :(得分:1)

想出来。需要准确指定此配置文件的位置,该应用程序捆绑时会发生更改(run-time information):

import sys, os

if getattr(sys, 'freeze', False):
    # running as bundle (aka frozen)
    bundle_dir = sys._MEIPASS
else:
    # running live
    bundle_dir = os.path.dirname(os.path.abspath(__file__))

app.config.from_pyfile(os.path.join(bundle_dir, 'config.py'))

通过pyinstaller生成包的命令也应该是:

pyinstaller --onefile --add-data './config.py:.'

抓取config.py并将其添加到捆绑应用的顶级(adding data files)。