Donfig is a Python library designed to simplify package and script configuration, drawing inspiration from the configuration logic originally found in the Dask library. It allows configuration through programmatic settings, environment variables, and YAML files located in standard paths. The library is actively maintained, with the current version being 0.8.1.post1, and releases occurring periodically to add features and fix bugs.
pip install donfigVerified import paths — ran on the pinned version, not inferred.
Initialize a `Config` object with a unique name, which it uses to locate environment variables and YAML files. Access settings using `config.get()` and update them with `config.set()`. The `set()` method can also be used as a context manager for temporary changes. Environment variables take precedence over YAML files, which in turn take precedence over programmatic defaults.
Review calls to `config.update_defaults()` and ensure the intended merging/overriding logic matches the new behavior. If preserving old defaults is crucial, manually check for existence before updating.
Be aware of the `ast.literal_eval` parsing when setting environment variables. Ensure values are formatted correctly as Python literals if specific types (like booleans, numbers, lists, dictionaries) are expected. For string values that should not be evaluated, ensure they are quoted or otherwise treated as plain strings by your application after retrieval if `ast.literal_eval` causes issues.
Maintain consistency in your key naming convention (either all hyphens or all underscores) to avoid confusion. If you use both, remember they will resolve to the same internal key.
Monitor application-specific documentation for deprecation warnings related to configuration keys. Update your configurations as recommended to use newer, non-deprecated keys.
Ensure all configuration sources (e.g., values passed to `Config` constructor, loaded from files, or environment variables) are dictionary-like objects. Review how configurations are loaded and processed to prevent strings from being mistakenly treated as dictionaries in the internal update logic. If string values are intended as configuration, they might need to be wrapped in a dictionary or processed differently before being passed to `donfig`.
Ensure all configuration sources (defaults, files, environment variables, etc.) that are intended to be merged by `donfig` are correctly formatted and parsed as dictionary-like objects before or during their loading. If a source unexpectedly yields a string, investigate how it's being produced and ensure it's converted to a dictionary or handled appropriately by Donfig's parsing mechanisms (e.g., by ensuring JSON strings are parsed as JSON objects).
Install the package using pip: `pip install donfig`
Ensure the 'my_setting' key is defined in a YAML file, as an environment variable (e.g., `MYPKG_MY__SETTING`), or explicitly set in the `Config` object, for example: `config = Config('mypkg', defaults={'my_setting': 'default_value'})`Carefully review the YAML file for syntax errors, paying close attention to indentation and proper key-value pair formatting. A YAML linter can help identify issues.
Define the key in your configuration sources, or use the `.get()` method for safe access with a default value, e.g., `value = config.get('some_other_setting', 'default_value')`.