Registry / serialization / anyconfig

anyconfig

JSON →
library0.16.0pypypi✓ verified 21d ago

AnyConfig is a Python library that provides a unified API to load and dump configuration files in various formats (e.g., JSON, YAML, INI, TOML, XML). It includes features like content merging, template processing (with Jinja2), data querying (with JMESPath), and JSON schema validation/generation. The current version is 0.15.1, and it maintains a moderate release cadence with several updates per year to introduce new features and address issues.

pip install anyconfig
INSTALL
IMPORT
SIG · ANYCONFIG
A
anyconfig
serializationpythonv0.16.0
Install
1.8s avg
Import
357ms
Disk
22MB
Pass rate
10/ 10
Env Coverage10 / 10
glibc
3.93.13
musl
3.93.13
Install & Compatibility
Where this runs
tested against v0.16.0 · 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.915 runs
installs and imports cleanly · install 0.0s · import 0.379s · 23.7MB
glibc
py 3.103.915 runs
installs and imports cleanly · install 1.8s · import 0.335s · 26MB
22MB installed
● package 22MB
Code
Verified usage

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

anyconfig
import anyconfig
import anyconfig

This quickstart demonstrates how to load a YAML configuration file using `anyconfig.load()`. It highlights automatic format detection by file extension and the use of Jinja2 templating to resolve environment variables within the configuration. It also shows how to dump the loaded configuration to a different format like JSON.

import anyconfig import os # Create a dummy YAML config file for demonstration config_content = """ app: name: MyWebApp version: 1.0.0 database: host: localhost port: 5432 user: dbuser password: ${DB_PASSWORD|default_secret} """ config_file_path = "./app_config.yaml" with open(config_file_path, "w") as f: f.write(config_content) # Set an environment variable for template processing (optional) os.environ['DB_PASSWORD'] = 'supersecurepassword' # Load the configuration, enabling template processing config = anyconfig.load(config_file_path, ac_template=True, ac_context=os.environ) print(f"Application Name: {config['app']['name']}") print(f"Database Host: {config['database']['host']}") print(f"Database Password: {config['database']['password']}") # Example of dumping to another format (e.g., JSON) output_json_path = "./app_config.json" anyconfig.dump(config, output_json_path, ac_parser='json', indent=2) print(f"Configuration dumped to {output_json_path}") # Clean up dummy files os.remove(config_file_path) os.remove(output_json_path)
anyconfig --version
Debug
Known issues
gotchaAnyConfig dynamically loads backend parsers. If a dependency for a specific format (e.g., `PyYAML` or `toml`) is not installed, that format will silently not be supported, which can lead to unexpected errors if not handled.
fix
Ensure all necessary backend packages for the formats you intend to use are installed. The official documentation lists the required dependencies for each format. For example, `pip install anyconfig[yaml,toml]`.
affects: All versions
gotchaTemplate processing (e.g., Jinja2) within configuration files is disabled by default to prevent unintended side effects. If your configuration files use templating features, they will not be rendered unless explicitly enabled.
fix
When calling `anyconfig.load()` or `anyconfig.loads()`, pass `ac_template=True` to enable template rendering. You may also provide a context dictionary via `ac_context` for template variables, e.g., `anyconfig.load(path, ac_template=True, ac_context={'VAR': 'value'})` or `ac_context=os.environ`.
affects: All versions
gotchaWhen dumping YAML files, using the default `PyYAML` backend may result in loss of comments, key order, and specific formatting (e.g., block vs. flow style). This is due to `PyYAML`'s limitations in round-trip preservation.
fix
Install `ruamel.yaml` (e.g., `pip install 'anyconfig[yaml]' ruamel.yaml`) and ensure it's available. `anyconfig` will prefer `ruamel.yaml` if installed, which offers superior round-trip preservation for YAML, retaining comments and ordering.
affects: All versions
gotchaWhen loading multiple configuration files, `anyconfig` employs a default recursive merge strategy (`anyconfig.MS_DICTS`). This might not be the desired behavior if a simple overwrite or a different merge logic is expected.
fix
Specify the desired merge strategy using the `ac_merge` parameter in `anyconfig.load()`. Available strategies include `anyconfig.MS_REPLACE` (later values replace earlier ones), `anyconfig.MS_NO_REPLACE` (earlier values are kept), and `anyconfig.MS_DICTS_AND_LISTS` (recursive merge for both dicts and lists).
affects: All versions
Upgrade
Version history
0.16.0latest on PyPI · released Aug 11, 2026
Audit
Dependencies
PyYAMLoptionalEnables YAML configuration file support. `ruamel.yaml` is preferred for YAML 1.2 and round-trip features.
ruamel.yamloptionalProvides enhanced YAML 1.2 support and round-trip capabilities (preserves comments, order, etc.) over PyYAML. Used if available.
tomloptionalEnables TOML configuration file support.
xmltodictoptionalEnables XML configuration file support.
msgpackoptionalEnables MessagePack configuration file support.
configobjoptionalEnables ConfigObj configuration file support.
python-magicoptionalUsed for content type detection when loading files, though often not strictly required if file extensions are reliable.
jinja2optionalRequired for processing configuration files as Jinja2 templates.
jmespathoptionalRequired for querying loaded configuration data using JMESPath expressions.
Agent activity
33 hits · last 30 days
node
27
OpenAI (training)
1
Resources