qinking126
qinking126

Reputation: 11885

Load Flask config from multiple files

I need to load configuration from multiple files. I use the code below to load one file. Should I use it repeatedly? How do I load multiple configs?

app = Flask(__name__)
app.config.from_object('yourapplication.default_settings')

Upvotes: 3

Views: 3958

Answers (1)

davidism
davidism

Reputation: 127210

You can load config however you want, the only requirement is that it ends up in app.config. from_object is just a helper method for if your config is in an importable Python file, such as the default settings in your example. There are other helpers as well, or you can just treat app.config as a dict and set values however you want.

The standard method for overriding default settings is to load them and then load local settings from the instance folder.

app = Flask(__name__, instance_relative_config=True)
app.config.from_object('myapp.default_settings')
app.config.from_pyfile('local_settings.py', silent=True)

This will load local_settings.py in the instance folder into the config, and ignore if the file doesn't exist.

myproject/
    myapp/
        __init__.py
        default_settings.py
    instance/
        local_settings.py

Access the config wherever you need it by importing the app.

from myapp import app
my_value = app.config['my_key']

Upvotes: 5

Related Questions