Methods to configure Flask App

Subscribe to my newsletter and never miss my upcoming articles

While configuring Flask Boilerplate App, I had a problem for configuring configuration.

So,I found some ways to do configuration for flask:

Since I wanted to use a for saving all the conf variables.

Method 1: Using Instance folder

Sometimes you’ll need to define configuration variables that contain sensitive information. We’ll want to separate these variables from those in and keep them out of the repository. You may be hiding secrets like database passwords and API keys, or defining variables specific to a given machine. To make this easy, Flask gives us a feature called instance folders. The instance folder is a sub-directory of the repository root and contains a configuration file specifically for this instance of the application. We don’t want to commit it into version control.

To load configuration variables from an instance folder, we use app.config.from_pyfile(). If we set instance_relative_config=True when we create our app with the Flask() call, app.config.from_pyfile() will load the specified file from the instance/ directory.

app = Flask(__name__, instance_relative_config=True)

Method 2: The simple case

A simple application may not need any of these complicated features. You may just need to put in the root of your repository and load it in or yourapp/

The file should contain one variable assignment per line. When your app is initialized, the variables in are used to configure Flask and its extensions are accessible via the app.config dictionary – e.g. app.config["DEBUG"] = True. or app/

from flask import Flask

app = Flask(__name__)

Now we can access the configuration variables via app.config["VAR_NAME"].

Method 3: Configuration Based on Environment Variables

The instance folder shouldn’t be in version control. This means that you won’t be able to track changes to your instance configurations. That might not be a problem with one or two variables, but if you have finely tuned configurations for various environments (production, staging, development, etc.) you don’t want to risk losing that.

Flask gives us the ability to choose a configuration file on load based on the value of an environment variable. This means that we can have several configuration files in our repository and always load the right one. Once we have several configuration files, we can move them to their own config directory.

In this listing we have different configuration files

config/ config/ config/ config/

To decide which configuration file to load, we'll call app.config.from_envvar()


app = Flask(__name__)

Load the file specified by the APP_CONFIG_FILE environment variable, Variables defined here will override those in the default configuration


The value of the environment variable should be the absolute path to a configuration file. How we set this environment variable depends on the platform in which we’re running the app. If we’re running on a regular Linux server, we can set up a shell script that sets our environment variables and runs

 $ export APP_CONFIG_FILE=/var/www/yourapp/config/
 $ python

That's it. Your Flask should run properly.

No Comments Yet