Pyaml-env provides a straightforward way to parse YAML configuration files, automatically resolving environment variables denoted by the `!ENV` tag. It's built on top of PyYAML and is currently at version 1.2.2. The library maintains an active release cadence, with updates typically addressing bug fixes, dependency compatibility, and feature enhancements a few times a year.
pip install pyaml-envVerified import paths — ran on the pinned version, not inferred.
This quickstart demonstrates how to load a YAML configuration file with embedded `!ENV` tags. It shows how environment variables are resolved and how to provide default values using a colon (`:`) separator. It also illustrates accessing the parsed configuration and cleans up after execution.
Update your YAML files to use the colon separator: `!ENV VAR:default_value`.
Instead of `!ENV VAR !!float`, use `!ENV VAR tag:yaml.org,2002:float`. For example, `!ENV DB_PORT:3306 tag:yaml.org,2002:int`.
Upgrade to v1.2.0 or newer. This version correctly restricts parsing only to values explicitly marked with `!ENV`.
Specify the encoding explicitly when calling `parse_config` using the `encoding` parameter, e.g., `parse_config('config.yaml', encoding='latin-1')`.Ensure the environment variable is set before running the application, or provide a default value in your YAML: `!ENV MY_ENVIRONMENT_VARIABLE:default_value`.
Carefully review the YAML file for syntax errors, paying close attention to indentation and character usage, especially around `!ENV` tags.
Verify that all intermediate keys exist in your YAML structure and resolve to mapping types. Ensure `!ENV` variables are correctly set or have defaults, preventing entire sections from becoming `None`.