EnvYAML is a Python library that simplifies reading YAML configuration files and seamlessly integrates environment variables. It allows referencing environment variables directly within YAML files using the `${VAR_NAME}` or `$VAR_NAME` syntax, providing a flexible and secure way to manage configurations. The library is currently at version 1.10.211231 and is actively maintained with regular updates, often focusing on bug fixes and minor feature enhancements.
pip install envyamlVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to create a simple YAML configuration file that references environment variables, load it using EnvYAML, and access the configuration values. It includes examples for mandatory and optional environment variables with default values.
To disable strict mode, initialize `EnvYAML('config.yaml', strict=False)` or set the `ENVYAML_STRICT_DISABLE` environment variable before initialization. Ensure all referenced environment variables are set if strict mode is active.Use `$$` for a literal `$` in your YAML configuration.
Always quote values that should be treated as strings, e.g., `country: "NO"` instead of `country: NO`, to prevent unintended boolean conversion.
Review configurations using `$` or `$$` notation, especially if upgrading from much older versions, to ensure they parse as intended. The current behavior (1.10.211231+) is more precise for escaped dollar signs.
Either define the missing environment variable, provide a default value in your YAML (e.g., `${VAR|default_value}`), or disable strict mode when initializing EnvYAML: `env = EnvYAML('config.yaml', strict=False)` or by setting the environment variable `ENVYAML_STRICT_DISABLE=1`.Install the package using pip: `pip install envyaml`.
Import the `EnvYAML` class directly from the `envyaml` package using `from envyaml import EnvYAML`.
Ensure the key path exists in your YAML file. You can also use the `.get()` method with a default value to prevent a KeyError: `value = env.get('some.undefined.key', 'default_value')`.