EnvYAML

1.10.211231 · active · verified Sat Apr 11

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.

Warnings

Install

Imports

Quickstart

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.

import os
from envyaml import EnvYAML

# Create a dummy YAML file
with open('config.yaml', 'w') as f:
    f.write('app:\n')
    f.write('  name: "${APP_NAME}"\n')
    f.write('  version: "${APP_VERSION|1.0.0}"\n')
    f.write('database:\n')
    f.write('  host: $DB_HOST\n')
    f.write('  port: $DB_PORT|5432\n')
    f.write('  password: $DB_PASSWORD
')

# Set environment variables (or they will default if specified in YAML)
os.environ['APP_NAME'] = 'MyAwesomeApp'
os.environ['DB_HOST'] = 'localhost'
os.environ['DB_PASSWORD'] = os.environ.get('DB_PASSWORD', 'super_secret_dev')

# Initialize EnvYAML
env = EnvYAML('config.yaml')

# Access configuration values
print(f"App Name: {env['app.name']}")
print(f"App Version: {env['app.version']}")
print(f"Database Host: {env['database.host']}")
print(f"Database Port: {env['database.port']}")
print(f"Database Password: {env['database.password']}")

# Clean up dummy file
os.remove('config.yaml')

view raw JSON →