Registry / serialization / config

config

JSON →
library0.5.1pypypi✓ verified 24d ago

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.

pip install config
INSTALL
IMPORT
SIG · CONFIG
C
config
serializationpythonv0.5.1
Install
1.5s avg
Import
29ms
Disk
16MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.5.1 · pip install
no network on importno background threads
Install × environment matrix
Each cell = how many times install + import succeeded across repeated harness runs. Partial = flaky.
glibc = Debian/Ubuntu slim · musl = Alpine Linux
musl
py 3.103.95 runs
installs and imports cleanly · install 0.0s · import 0.032s · 17.9MB
glibc
py 3.103.95 runs
installs and imports cleanly · install 1.5s · import 0.026s · 18MB
16MB installed
● package 16MB
Code
Verified usage

Verified import paths — ran on the pinned version, not inferred.

Config
from config import Config
config module functions
import config
The module may also expose top-level functions, such as `config.create()` mentioned in some examples, for simpler setup without direct `Config` class instantiation.

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)
Debug
Known issues
breakingThe 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.
fix
Review and update existing `.cfg` files to conform to the latest CFG format syntax, particularly for boolean/null literals and cross-reference patterns.
affects: Versions 0.5.x and later, if upgrading from very old versions (pre-0.5.0) that used a different CFG format specification.
gotchaThe 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.
fix
Always ensure configuration files come from trusted sources. Sanitize or validate complex configuration values if there's any doubt about their origin or content. Avoid allowing arbitrary object instantiation or method calls if security is paramount and config sources are not fully controlled.
affects: All versions
gotchaAs 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.
fix
Utilize environment variables (e.g., `os.environ.get('SECRET_KEY')`) or a dedicated secret management solution for sensitive data. Use environment variable fallbacks in your configuration files as demonstrated in the quickstart (e.g., `password: ${DB_PASSWORD|}`).
affects: All versions
Errors
Common errors & fixes
ModuleNotFoundError: No module named 'config'
The 'config' package (version 0.5.1) is either not installed in your Python environment, or a local file or directory named 'config.py' or 'config/' is shadowing the installed library, causing Python to find the local file instead of the package.
fix
Install the package using `pip install config`. If the package is already installed, check your project directory and Python path for any local files or folders named 'config' and rename them to avoid conflicts. Ensure your virtual environment is activated if you are using one.
AttributeError: module 'config' has no attribute 'Config'
This error occurs when you attempt to access a `Config` class directly from the top-level `config` module (e.g., `config.Config()`). The `config` library (version 0.5.1) by Vinay Sajip exposes its main configuration class as `Configuration` within its `config.config` submodule, not directly under `config`.
fix
Correctly import the `Configuration` class from the `config.config` submodule: `from config.config import Configuration`. Then, instantiate it as `cfg = Configuration()` and load your configuration files, for example, `cfg.add_file('your_config.ini')`.
KeyError: 'your_config_key'
You are attempting to access a configuration item using a key that does not exist in the currently loaded configuration.
fix
Verify that 'your_config_key' is correctly defined in your configuration file (e.g., `your_config.ini`, `your_config.json`, or a dictionary used to initialize `Configuration`). To safely access optional configuration items, use the `get()` method with a default value, like `value = cfg.get('your_config_key', 'default_value')`.
Upgrade
Version history
0.5.1latest on PyPI · released Sep 11, 2021
Audit
Dependencies

No dependency data recorded yet.

Agent activity
14 hits · last 30 days
node
10
OpenAI (training)
1
Resources