1

I have a very basic flask application which imports a configuration file like so:

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

I'm trying to bundle this flask application using PyInstaller, which analyzes all imports to create its list of dependencies. Because config.py isn't explicitly imported, I believe I need to add it as an additional data-file. Per this SO question I've tried the following:

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

When I run my bundled executable, however, I get the following error:

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

Everything works fine when I explicitly import the configuration file:

from config import *

But I'd rather figure out why my initial method isn't working. Note I'm running macOS 10.12.5 and python 3.4.5

Johnny Metz
  • 5,977
  • 18
  • 82
  • 146

1 Answers1

3

Figured it out. Need to specify exactly where this configuration file is located, which changes when the app is bundled (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'))

Also the command to generate the bundle via pyinstaller should be:

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

which grabs config.py and adds it to the top level of the bundled app (adding data files).

Johnny Metz
  • 5,977
  • 18
  • 82
  • 146