{"id":1465,"library":"donfig","title":"Donfig","description":"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.","status":"active","version":"0.8.1.post1","language":"en","source_language":"en","source_url":"https://github.com/pytroll/donfig","tags":["configuration","settings","yaml","environment-variables"],"install":[{"cmd":"pip install donfig","lang":"bash","label":"Install stable version"}],"dependencies":[{"reason":"Required for parsing configuration from YAML files.","package":"pyyaml","optional":false}],"imports":[{"symbol":"Config","correct":"from donfig import Config"}],"quickstart":{"code":"import os\nfrom donfig import Config\n\n# Simulate environment variable for demonstration\nos.environ['MYAPP_SETTING_ONE'] = 'env_value'\n\n# Create a configuration object for your application/package\n# The name ('myapp' here) is used for environment variable prefixing (e.g., MYAPP_)\n# and YAML file searching (e.g., ~/.config/myapp/)\nconfig = Config('myapp', defaults={'setting_one': 'default_value', 'setting_two': 123})\n\n# Access configuration values\nvalue_one = config.get('setting_one')\nvalue_two = config.get('setting_two')\n\nprint(f\"Setting One (from env): {value_one}\")\nprint(f\"Setting Two (from default): {value_two}\")\n\n# Update configuration programmatically\nconfig.set(setting_two=456)\nprint(f\"Setting Two (updated): {config.get('setting_two')}\")\n\n# Use as a context manager to temporarily change configuration\nwith config.set(setting_one='context_value'):\n    print(f\"Setting One (in context): {config.get('setting_one')}\")\n\nprint(f\"Setting One (after context): {config.get('setting_one')}\")\n\n# Clean up environment variable (optional, for isolated testing)\ndel os.environ['MYAPP_SETTING_ONE']","lang":"python","description":"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."},"warnings":[{"fix":"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.","message":"The `update_defaults` method in `Config` objects changed behavior in v0.8.0. Previously, it might not have consistently overridden existing default values. As of v0.8.0, `update_defaults` will reliably override old default values, which might change behavior if your code relied on the previous 'buggy' non-overriding behavior.","severity":"breaking","affected_versions":">=0.8.0"},{"fix":"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.","message":"When loading configuration from environment variables, Donfig uses `ast.literal_eval` to parse values. This means that environment variable values are interpreted as Python literals (e.g., 'True' becomes `True` boolean, '123' becomes `123` integer, '[1, 2]' becomes a list). This can lead to unexpected type conversions if not accounted for.","severity":"gotcha","affected_versions":"All versions"},{"fix":"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.","message":"When setting configuration values using `config.set()`, underscores (`_`) and hyphens (`-`) in key names are treated as identical. For example, `config.set({'my-key': True})` is equivalent to `config.set({'my_key': True})`.","severity":"gotcha","affected_versions":"All versions"},{"fix":"Monitor application-specific documentation for deprecation warnings related to configuration keys. Update your configurations as recommended to use newer, non-deprecated keys.","message":"Version 0.8.0 introduced support for key deprecation. While not a direct breaking change for existing configurations, this means that future releases of packages using donfig might mark certain configuration keys as deprecated. It's advisable to check documentation for specific applications using donfig to see if any keys you rely on have been deprecated.","severity":"deprecated","affected_versions":">=0.8.0"}],"env_vars":null,"last_verified":"2026-04-09T00:00:00.000Z","next_check":"2026-07-08T00:00:00.000Z"}