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 anyconfigVerified import paths — ran on the pinned version, not inferred.
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.
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]`.
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`.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.
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).