Flask provides a config
object which is a subclass of a Python dictionary. It stores all the configuration variables for your application. You can access and modify these variables using dictionary - like operations. For example:
from flask import Flask
app = Flask(__name__)
# Setting a configuration variable
app.config['DEBUG'] = True
# Accessing a configuration variable
debug_mode = app.config['DEBUG']
.py
, .ini
, or .json
files to store configuration variables. Flask allows you to load these files into the config
object.In the development environment, you may want to enable debugging mode, use a local database, and have more verbose logging. For example, you can set the DEBUG
flag to True
so that Flask will show detailed error messages when something goes wrong.
When running tests, you may want to use a separate test database to avoid affecting the production data. You can also configure different logging levels for testing.
In production, you need to disable debugging mode, use a production - grade database, and ensure that sensitive information like API keys is securely stored.
config.py
)# config.py
DEBUG = True
DATABASE_URI = 'sqlite:///development.db'
SECRET_KEY = 'development_secret_key'
from flask import Flask
app = Flask(__name__)
# Load the configuration file
app.config.from_pyfile('config.py')
@app.route('/')
def index():
return f"Debug mode: {app.config['DEBUG']}, Database URI: {app.config['DATABASE_URI']}"
if __name__ == '__main__':
app.run()
export DEBUG=False
export DATABASE_URI='postgresql://user:password@localhost/production.db'
export SECRET_KEY='production_secret_key'
import os
from flask import Flask
app = Flask(__name__)
# Load configuration from environment variables
app.config['DEBUG'] = os.environ.get('DEBUG', False)
app.config['DATABASE_URI'] = os.environ.get('DATABASE_URI')
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
@app.route('/')
def index():
return f"Debug mode: {app.config['DEBUG']}, Database URI: {app.config['DATABASE_URI']}"
if __name__ == '__main__':
app.run()
Storing sensitive information like API keys and database passwords directly in your Python code is a major security risk. If your code is accidentally made public, these secrets will be exposed.
Failing to isolate different environments can lead to issues such as test data interfering with production data or development settings being used in production.
Using too many configuration sources or overly complex configuration logic can make your application hard to understand and maintain.
Store sensitive information like API keys, database passwords, and secret keys as environment variables. This ensures that they are not hard - coded in your source code.
Create separate configuration files for development, testing, and production. This makes it easy to manage different settings for each environment.
Set a default configuration in your Python code and then override it with environment - specific settings. This provides a fallback in case some configuration variables are not set.
from flask import Flask
app = Flask(__name__)
# Default configuration
app.config.from_mapping(
DEBUG=False,
DATABASE_URI='sqlite:///default.db',
SECRET_KEY='default_secret_key'
)
# Load environment - specific configuration
if app.env == 'development':
app.config.from_pyfile('development_config.py')
elif app.env == 'production':
app.config.from_pyfile('production_config.py')
@app.route('/')
def index():
return f"Debug mode: {app.config['DEBUG']}, Database URI: {app.config['DATABASE_URI']}"
if __name__ == '__main__':
app.run()
Flask configuration management for different environments is essential for building robust and secure applications. By understanding the core concepts, typical usage scenarios, avoiding common pitfalls, and following best practices, you can ensure that your Flask application runs smoothly across various environments. Whether you are developing, testing, or deploying to production, proper configuration management will save you time and prevent potential security risks.