{"id":5848,"library":"anyconfig","title":"AnyConfig","description":"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.","status":"active","version":"0.15.1","language":"en","source_language":"en","source_url":"https://github.com/ssato/python-anyconfig","tags":["configuration","config-files","json","yaml","ini","toml","xml","schema-validation","templating","merge"],"install":[{"cmd":"pip install anyconfig","lang":"bash","label":"Base Installation"},{"cmd":"pip install anyconfig[yaml,toml,xml,msgpack,configobj,bson,ini,properties]","lang":"bash","label":"Installation with common backends"},{"cmd":"pip install 'anyconfig[yaml]==0.15.1' 'ruamel.yaml<0.18'","lang":"bash","label":"Install with ruamel.yaml for YAML 1.2 and round-trip support"}],"dependencies":[{"reason":"Enables YAML configuration file support. `ruamel.yaml` is preferred for YAML 1.2 and round-trip features.","package":"PyYAML","optional":true},{"reason":"Provides enhanced YAML 1.2 support and round-trip capabilities (preserves comments, order, etc.) over PyYAML. Used if available.","package":"ruamel.yaml","optional":true},{"reason":"Enables TOML configuration file support.","package":"toml","optional":true},{"reason":"Enables XML configuration file support.","package":"xmltodict","optional":true},{"reason":"Enables MessagePack configuration file support.","package":"msgpack","optional":true},{"reason":"Enables ConfigObj configuration file support.","package":"configobj","optional":true},{"reason":"Used for content type detection when loading files, though often not strictly required if file extensions are reliable.","package":"python-magic","optional":true},{"reason":"Required for processing configuration files as Jinja2 templates.","package":"jinja2","optional":true},{"reason":"Required for querying loaded configuration data using JMESPath expressions.","package":"jmespath","optional":true}],"imports":[{"symbol":"anyconfig.load","correct":"import anyconfig\nconfig = anyconfig.load('config.yaml')"},{"symbol":"anyconfig.dump","correct":"import anyconfig\nanyconfig.dump(data, 'output.json')"},{"symbol":"anyconfig.loads","correct":"import anyconfig\nconfig = anyconfig.loads('key: value', ac_parser='yaml')"},{"symbol":"anyconfig.dumps","correct":"import anyconfig\nconfig_str = anyconfig.dumps({'key': 'value'}, ac_parser='json')"}],"quickstart":{"code":"import anyconfig\nimport os\n\n# Create a dummy YAML config file for demonstration\nconfig_content = \"\"\"\napp:\n  name: MyWebApp\n  version: 1.0.0\ndatabase:\n  host: localhost\n  port: 5432\n  user: dbuser\n  password: ${DB_PASSWORD|default_secret}\n\"\"\"\n\nconfig_file_path = \"./app_config.yaml\"\nwith open(config_file_path, \"w\") as f:\n    f.write(config_content)\n\n# Set an environment variable for template processing (optional)\nos.environ['DB_PASSWORD'] = 'supersecurepassword'\n\n# Load the configuration, enabling template processing\nconfig = anyconfig.load(config_file_path, ac_template=True, ac_context=os.environ)\n\nprint(f\"Application Name: {config['app']['name']}\")\nprint(f\"Database Host: {config['database']['host']}\")\nprint(f\"Database Password: {config['database']['password']}\")\n\n# Example of dumping to another format (e.g., JSON)\noutput_json_path = \"./app_config.json\"\nanyconfig.dump(config, output_json_path, ac_parser='json', indent=2)\nprint(f\"Configuration dumped to {output_json_path}\")\n\n# Clean up dummy files\nos.remove(config_file_path)\nos.remove(output_json_path)\n","lang":"python","description":"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."},"warnings":[{"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]`.","message":"AnyConfig 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.","severity":"gotcha","affected_versions":"All versions"},{"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`.","message":"Template 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.","severity":"gotcha","affected_versions":"All versions"},{"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.","message":"When 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.","severity":"gotcha","affected_versions":"All versions"},{"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).","message":"When 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.","severity":"gotcha","affected_versions":"All versions"}],"env_vars":null,"last_verified":"2026-04-14T00:00:00.000Z","next_check":"2026-07-13T00:00:00.000Z"}