Flask Environment Specific Configurations

Last Updated : 4 Jun, 2026

When developing a Flask application, different environments such as development, testing, and production require different settings. For example, in development, we may want debugging enabled, while in production, we need stricter security settings. Flask allows us to configure our application based on the environment, ensuring smooth transitions from development to deployment.

Steps to Set Environment-Specific Configurations

Flask provides multiple ways to configure the application based on the environment. some of them are:

  1. Using Configuration Objects (e.g., config.py)
  2. Loading from Environment Variables
  3. Using Flask.config.from_object() or Flask.config.from_envvar()

Let's go through each method in detail.

1. Using Configuration Objects

A common approach is to create a separate config.py file and define different configurations as Python classes. Below is the code for it:

config.py

Python
class Config:
    DEBUG = False
    TESTING = False
    SECRET_KEY = 'your_secret_key'
    SQLALCHEMY_DATABASE_URI = 'sqlite:///default.db'

class DevelopmentConfig(Config):
    DEBUG = True
    SQLALCHEMY_DATABASE_URI = 'sqlite:///development.db'

class TestingConfig(Config):
    TESTING = True
    SQLALCHEMY_DATABASE_URI = 'sqlite:///testing.db'

class ProductionConfig(Config):
    SQLALCHEMY_DATABASE_URI = 'postgresql://user:password@localhost/production_db'

Explanation:

  • Config is a base class that holds default settings.
  • DevelopmentConfig, TestingConfig, and ProductionConfig inherit from Config and override specific settings based on the environment.

Note: In production environments, sensitive values such as secret keys should be loaded from environment variables instead of being hardcoded.

Now to apply a specific configuration in our Flask app we can use:

Python
from flask import Flask
from config import DevelopmentConfig, ProductionConfig, TestingConfig

app = Flask(__name__)
app.config.from_object(DevelopmentConfig)  

Here we have applied the DevelopmentConfig environment to the app and similarly, using app.config.from_object() we can apply different environment settings whenver required.

2. Loading Configurations from Environment Variables

Another way to configure a Flask application is by using environment variables. This method helps keep sensitive information, such as secret keys and database credentials, secure while allowing different settings to be used across development, testing, and production environments.

Python
import os
from flask import Flask

app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'default_secret')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'sqlite:///default.db')

Now we can set the environment variables in the terminal using these commands:

export SECRET_KEY='my_production_secret'
export DATABASE_URL='postgresql://user:password@localhost/prod_db'

3. Loading Configurations Dynamically in Flask

Flask provides built-in methods such as from_object() and from_envvar() for loading configuration settings dynamically. These methods simplify configuration management across different environments.

from_object()

This method loads configuration settings from a Python object or module, making it useful when managing multiple environment configurations.

Python
app.config.from_object('config.ProductionConfig')  # Loads settings from ProductionConfig

Here, config.ProductionConfig refers to the ProductionConfig class inside the config.py file. we can change this dynamically by passing a different class, such as DevelopmentConfig or TestingConfig, based on the environment.

from_envvar()

Instead of hardcoding configuration file names, we can store the file path in an environment variable and use from_envvar() to load it.

Python
import os
app.config.from_envvar('FLASK_CONFIG_FILE')

Example Configuration File

Create a configuration file named settings.py:

DEBUG = False
TESTING = False
SECRET_KEY = 'my-secret-key'
SQLALCHEMY_DATABASE_URI = 'sqlite:///production.db'

The from_envvar() method reads the file path stored in the environment variable and loads all configuration settings defined in that file.

Before running the app, set the environment variable in the terminal using command :

export FLASK_CONFIG_FILE='/path/to/settings.py'

Comment