Flask Environment Specific Configurations
Last Updated :
24 May, 2025
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. The most common methods include:
- Using Configuration Objects (e.g., config.py)
- Loading from Environment Variables
- 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.
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) # Change based on environment
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
We can also set environment-specific configurations using system environment variables. This is useful when deploying the application to cloud services where secrets and database credentials should not be hardcoded. Here's an example:
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. Using Flask.config.from_object() and Flask.config.from_envvar()
Flask provides built-in methods to load configurations dynamically. Instead of manually specifying configurations in the code, we can use from_object() and from_envvar(). Let's look at them one by one with examples.
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')
Before running the app, set the environment variable in the terminal using command :
export FLASK_CONFIG_FILE='/path/to/config.py'