Config
Config is a Python module that provides a hierarchical, easy-to-use, and powerful configuration scheme. It supports mappings, sequences, cross-references between configuration parts, flexible access to Python objects, includes, simple expression evaluation, and the ability to change, save, cascade, and merge configurations. It also interfaces easily with environment variables and command-line options. The current version is 0.5.1 and it appears to be actively maintained, with the latest release in September 2021 and recent mentions in 2025-2026 as stable.
Warnings
- breaking The underlying CFG configuration format used by this library introduced changes in syntax. Specifically, boolean and null literals changed from Python's `True`, `False`, `None` to JSON-compatible `true`, `false`, `null`. Also, cross-references evolved from `$A.B.C` to `${A.B.C}` for improved expressivity. Older configuration files using the deprecated syntax may fail to parse or produce unexpected results with newer versions of the library that adhere to the updated CFG format specification.
- gotcha The library offers 'flexible access to real Python objects without full-blown eval()' as a feature. While this avoids direct `eval()`, constructing complex Python objects or invoking methods based on configuration values can still pose a security risk if configuration files are sourced from untrusted inputs, potentially leading to unintended code execution or resource manipulation.
- gotcha As with any configuration management system, storing sensitive information (e.g., API keys, database credentials) directly in plain text configuration files is a security risk. While the library supports environment variable integration, hardcoding secrets is a common footgun.
Install
-
pip install config
Imports
- Config
from config import Config
- config module functions
import config
Quickstart
import os
from config import Config
# Create a dummy config file for demonstration
config_content = """
[DEFAULT]
log_level: INFO
data_path: /var/data
[server]
host: 127.0.0.1
port: ${server.default_port|8080}
base_url: http://${server.host}:${server.port}
[database]
type: postgres
host: db.example.com
user: ${DB_USER|guest}
password: ${DB_PASSWORD|}
"""
with open('example.cfg', 'w') as f:
f.write(config_content)
# Set environment variables for demonstration
os.environ['DB_USER'] = 'admin'
os.environ['DB_PASSWORD'] = os.environ.get('TEST_DB_PASSWORD', 's3cr3t_p@ssw0rd')
# Load the configuration
cfg = Config('example.cfg')
# Access configuration values
print(f"Log Level: {cfg.log_level}")
print(f"Server Host: {cfg.server.host}")
print(f"Server Port: {cfg.server.port}")
print(f"Base URL: {cfg.server.base_url}")
print(f"DB User: {cfg.database.user}")
print(f"DB Password: {cfg.database.password}")
# Clean up the dummy config file
os.remove('example.cfg')
# Example of overriding a default with an environment variable
# The config file specifies `default_port: 8080`, but the example config uses a direct reference.
# Let's show how an environment variable for `DB_USER` works.
# Expected output for DB User: admin (from environment variable)
# Expected output for DB Password: s3cr3t_p@ssw0rd (from environment variable, if TEST_DB_PASSWORD is not set)