Flask Config Configuration Class API
Config is a dictionary subclass used to manage all configuration items for Flask applications.
Throughapp.configAccess, with support for loading configuration from multiple sources.
Config class methods
| Methods | Signature | Description |
|---|---|---|
| from_pyfile | (filename, silent=False) | Load configuration from a Python file. When silent=True, file-not-found errors are ignored. |
| from_object | (obj) | Load configuration from an object (module, class, string import path). Only uppercase attributes are loaded. |
| from_envvar | (variable_name, silent=False) | Load configuration from a file pointed to by an environment variable. Equivalent to from_pyfile(os.environ[var]). |
| from_prefixed_env | (prefix="FLASK", *, loads=json.loads) | Load from environment variables. Reads variables with the FLASK_ prefix, removes the prefix, and sets them as configuration. |
| from_file | (filename, load, silent=False, text=True) | Load from files in any format. "load" is the parsing function (e.g., json.load, tomllib.load). |
| from_mapping | (mapping=None, **kwargs) | Load configuration from a mapping object. Optional **kwargs key-value pairs. Only uppercase keys are loaded. |
| get_namespace | (namespace, lowercase=True, trim_namespace=True) | Get a subset of configuration with a specified prefix. For example, get_namespace("DB_") retrieves all database configuration. |
Loading method comparison
| Loading methods | Applicable scenarios | Example |
|---|---|---|
| from_pyfile | Configuration files are in Python format. | app.config.from_pyfile("config.py") |
| from_object | Use classes or modules to organize configuration and distinguish between environments. | app.config.from_object("myapp.config.ProductionConfig") |
| from_prefixed_env | 12-Factor App, environment variable configuration. | app.config.from_prefixed_env() |
| from_file | JSON, TOML, and other non-Python formats | app.config.from_file("config.json", load=json.load) |
| get_namespace | Extract the configuration subset and pass it to the library's constructor. | db_config = app.config.get_namespace("DB_") |
Configuration key naming rules
Configuration Key NamesMust be entirely uppercaseThis is a Flask convention; methods such as from_object, from_mapping, and from_prefixed_env automatically ignore lowercase keys.
Code Examples
Example
from flask import Flask
app = Flask(__name__)
# Method 1: Load from Python file
app.config.from_pyfile("config.py")
# Method 2: Load from environment variables (read FLASK_ prefix)
app.config.from_prefixed_env()
# Method 3: Set directly
app.config["MY_SETTING"] = "value"
# Method 4: Load from JSON
import json
app.config.from_file("config.json", load=json.load)
# Method 5: Get namespace configuration
# Suppose there are configurations such as DB_HOST, DB_PORT, DB_NAME
db_config = app.config.get_namespace("DB_")
# db_config = {"host": "...", "port": ..., "name": "..."}
app = Flask(__name__)
# Method 1: Load from Python file
app.config.from_pyfile("config.py")
# Method 2: Load from environment variables (read FLASK_ prefix)
app.config.from_prefixed_env()
# Method 3: Set directly
app.config["MY_SETTING"] = "value"
# Method 4: Load from JSON
import json
app.config.from_file("config.json", load=json.load)
# Method 5: Get namespace configuration
# Suppose there are configurations such as DB_HOST, DB_PORT, DB_NAME
db_config = app.config.get_namespace("DB_")
# db_config = {"host": "...", "port": ..., "name": "..."}