Skip to main content

Simple YAML configuration file parser with easy access for structured data

Project description

EnvYAML | Easy configuration file parser for structured data

Latest Version EnvYaml CI/CD Pipeline Coverage Status Versions Code Style Alerts Code Quality License


Modern configuration files become to be more and more complex, flexible, and readable. YAML file format is perfect to store configuration but had no option to pass environment variables. They give flexibility, readability and provide an option to store complex data structure. This project goal is to simplify usage of the YAML file and environment variables as program configuration files with easy config key access.


pip install envyaml

Basic usage

Let's assume we had a project with this config file env.yaml

# env.yaml

    host: $DATABASE_HOST
    port: 3301
    username: username
    password: $DATABASE_PASSWORD
    database: test

      user: table_user
      blog: table_blog

    query: |-
      SELECT * FROM "users" WHERE "user" = $1 AND "login" = $2 AND "pwd" = $3

    insert: |-
      INSERT INTO "{table}" (user, login) VALUES ($1, $2)

    host: $REDIS_HOST|
    port: 5040
    db: $REDIS_DB|3 # with default value

      expire: 300
      prefix: $REDIS_PREFIX

escaped: $$.extra


Environment variables set to


Parse file with EnvYAML

from envyaml import EnvYAML

# read file env.yaml and parse config
env = EnvYAML('env.yaml')

# access project name

# >> simple-hello-42

# access whole database section

# {
# 'database': 'test',
# 'host': '',
# 'password': 'super-secret-password',
# 'port': 3301,
# 'table':
#   {
#       'blog': 'table_blog',
#       'user': 'table_user'
#   },
# 'username': 'username'
# }

# access database host value as key item

# >>

# access database user table value as key item

# >> table_user

# get sql query with $1,$2,$3 variables

# >> SELECT * FROM "users" WHERE "user" = $1 AND "login" = $2 AND "pwd" = $3

# using default values if variable not defined
# one example is redis host and redis port, when $REDIS_HOST not set then default value will be used

# >>

# one example is redis host and redis port, when $REDIS_DB not set then default value will be used

# >> 3

# access list items by number

# >> one

# access list items by number as key

# >> two

# test if you have key
print('redis.port' in env)

# >> True

Access config with get function and default value

print(env.get('not.exist.value', 'default'))
# >> default

print(env.get('empty_env', 'default'))
# >> default

# >> None

Use format function to update placeholder

print(env.format('database.insert', table="users"))
# >> INSERT INTO "users" (user, login) VALUES ($1, $2)

Strict mode

This mode is enable by default and prevents from declaring variables that do not exist in environment variables or .env file. This leads to having runtime ValueError exception when variables do not define with message Strict mode enabled, variable $VAR not defined!. To disable strict mode specify strict=False at EnvYAML object initialization. Another option to disable strict mode is to define ENVYAML_STRICT_DISABLE environment variable before initializing EnvYAML object.

Escaped variables

In case of usage $ in env.yaml file as value double $$ should be used. Example: Use escaped variable

# >> $.extra


MIT licensed. See the LICENSE file for more details.

Project details

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

envyaml-1.10.211231.tar.gz (7.6 kB view hashes)

Uploaded source

Built Distribution

envyaml-1.10.211231-py2.py3-none-any.whl (8.1 kB view hashes)

Uploaded py2 py3

Supported by

AWS AWS Cloud computing Datadog Datadog Monitoring Facebook / Instagram Facebook / Instagram PSF Sponsor Fastly Fastly CDN Google Google Object Storage and Download Analytics Huawei Huawei PSF Sponsor Microsoft Microsoft PSF Sponsor NVIDIA NVIDIA PSF Sponsor Pingdom Pingdom Monitoring Salesforce Salesforce PSF Sponsor Sentry Sentry Error logging StatusPage StatusPage Status page