Organize A Flask Project To Handle Production And Development Environments Effectively

Last modified:

Separated configurations for Development and Production

In most situations, applications would need different configurations for each environment: Production, Staging, Development, etc.

Load default config and an environment specific one

The easiest way to have multiple configurations is to:

  • use a default configuration that is always loaded and
  • the default configuration is under version control (e.g.: part of the git project repo) and
  • Use an environment variable to switch between the configurations that overrides the values as needed in each environment


Having a configuration that loads configuration values from the specified config environment YOURAPPLICATION_SETTINGS:

app = Flask(__name__)

Then we create a separate for each env and export the configuration path:

$ export

This way it will always load the default configs and then our custom variables from YOURAPPLICATION_SETTINGS.

Instance paths

From Flask 0.8, there is the concept of instance folders. The instance folder is a good fit for configuration files because it is designed to not be under version control and be deployment specific.

To keep the instance folder out of versioning in Git add instance/ to /.gitignore.

By default, Flask looks for a folder named instance in the same level of your main file or the package.


Using this folder and having environment specific configuration files in the config folder we end up having the following directory structure:


To have the configurations loaded we have to:

  • load the default config in config/
  • load configuration from instance folder instance/
  • Load the file specified by the APP_CONFIG_FILE environment variable

In myapp/ we load them:

# myapp/

app = Flask(__name__, instance_relative_config=True)

Then set the environment variable depending on the server we are invoking the app:

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


Now your folder structure should look like:

β”œβ”€β”€ config
β”‚   β”œβ”€β”€     # empty
β”‚   β”œβ”€β”€
β”‚   β”œβ”€β”€
β”‚   └──
β”œβ”€β”€ instance
β”‚   └──
└── myapp

It can be useful to also add support for dotenv to handle sensitive variables when deploying to Heroku.


Marcelo Canina
I'm Marcelo Canina, a developer from Uruguay. I build websites and web-based applications from the ground up and share what I learn here.
comments powered by Disqus

How to configure Flask to handle different configuration files based on environment variables. Having specific configuration files for production and development.

Clutter-free software concepts.
Translations English EspaΓ±ol

Except as otherwise noted, the content of this page is licensed under CC BY-NC-ND 4.0 . Terms and Policy.

Powered by SimpleIT Hugo Theme