Config

0.5.1 · active · verified Sun Apr 12

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

Install

Imports

Quickstart

This quickstart demonstrates how to load a configuration from a file using the `Config` class. It shows how to define sections, key-value pairs, references to other configuration values (e.g., `base_url`), and how to inject values from environment variables (e.g., `DB_USER`, `DB_PASSWORD`) with optional default fallbacks.

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)

view raw JSON →