pyyaml_env_tag
A custom YAML tag for referencing environment variables in YAML files.
Installation
Install the pyyaml_env_tag package with pip:
pip install pyyaml_env_tag
Enabling the tag
To enable the tag, pass your loader of choice into the add_env_tag function, which will
return the loader with the construstor added to it.
import yaml
from yaml_env_tag import add_env_tag
myLoader = add_env_tag(yaml.Loader)
Then you may use the loader as per usual. For example:
yaml.load(data, Loader=myLoader)
The add_env_tag is a high level helper function. If you need lower level access, you may
add the constructor (yaml_env_tag.construct_env_tag) to the loader directly using the
add_constructor method of the loader. Note that this requires that the tag (!ENV) be
defined as well.
from yaml_env_tag import construct_env_tag
Loader.add_constructor('!ENV', construct_env_tag)
Using the tag
Include the tag !ENV followed by the name of an environment variable in a YAML
file and the value of the environment variable will be used in its place.
key: !ENV SOME_VARIABLE
If SOME_VARIABLE is set to A string!, then the above YAML would result in the
following Python object:
{'key': 'A string!'}
The content of the variable is parsed using YAML's implicit scalar types, such as
string, bool, integer, float, datestamp and null. More complex types are not
recognized and simply passed through as a string. For example, if SOME_VARIABLE
was set to the string true, then the above YAML would result in the following:
{'key': True}
If the variable specified is not set, then a null value is assigned as a default.
You may define your own default as the last item in a sequence.
key: !ENV [SOME_VARIABLE, default]
In the above example, if SOME_VARIABLE is not defined, the string default would
be used instead, as follows:
{'key': 'default'}
You may list multiple variables as fallbacks. The first variable which is set is used. In any sequance with more than one item, the last item must always be a default value and will not be resolved as an environment variable.
key: !ENV [SOME_VARIABLE, FALLBACK, default]
As with variable contents, the default is resolved to a Python object of the implied type (string, bool, integer, float, datestamp and null).
When SOME_VARIABLE is not set, all four of the following items will resolve to
the same value (None):
- !ENV SOME_VARIABLE
- !ENV [SOME_VARIABLE]
- !ENV [SOME_VARIABLE, ~]
- !ENV [SOME_VARIABLE, null]
Related
pyyaml_env_tag was inspired by the Ruby package yaml-env-tag.
An alternate method of referencing environment variables in YAML files is implemented by pyyaml-tags and python_yaml_environment_variables. Each of those libraries use a template string and replace the template tag with the content of the variable. While this allows a single value to reference multiple variables and to contain additional content, it restricts all values to strings only and does not provide a way to define defaults.
License
pyyaml_env_tag is licensed under the MIT License as defined in LICENSE.
Changelog
[1.1] - 2025-05-13
- Ensure tests get included with distribution (#9).
[1.0] - 2025-05-09
- Add the
add_env_taghelper function as a higher level way of modifying the loader.
[0.1] - 2020-11-11
The initial release.
Release files for pyyaml-env-tag 1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| pyyaml_env_tag-1.1.tar.gz | 5.7 kB | Details |
Built distribution (wheel)
| File | Interpreter | ABI | Platform | Reset |
|---|---|---|---|---|
| pyyaml_env_tag-1.1-py3-none-any.whl | Python 3 | none | any | Details |
Total release size: 10.5 kB
Release files / pyyaml_env_tag-1.1.tar.gz
| Download URL | pyyaml_env_tag-1.1.tar.gz |
|---|---|
| Size | 5.7 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
2eb38b75a2d21ee0475d6d97ec19c63287a7e140231e4214969d0eac923cd7ff
|
|
BLAKE2b-256 checksum How to use checksums |
eb2e79c822141bfd05a853236b504869ebc6b70159afc570e1d5a20641782eaa
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 13, 2025.
Transparency logRelease files / pyyaml_env_tag-1.1-py3-none-any.whl
| Download URL | pyyaml_env_tag-1.1-py3-none-any.whl |
|---|---|
| Size | 4.7 kB |
| Tags | Python 3 |
|
SHA-256 checksum How to use checksums |
17109e1a528561e32f026364712fee1264bc2ea6715120891174ed1b980d2e04
|
|
BLAKE2b-256 checksum How to use checksums |
0411432f32f8097b03e3cd5fe57e88efb685d964e2e5178a48ed61e841f7fdce
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/6.1.0 CPython/3.12.9
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on May 13, 2025.
Transparency log