Envier

0.6.1 · active · verified Sun Mar 29

Envier is a Python library for extracting configuration from environment variables in a declarative and 12-factor-app-compliant way. It focuses on type-hinting and clear definition of environment variable mappings to Python objects. The library is currently at version 0.6.1 and is actively maintained with regular releases.

Warnings

Install

Imports

Quickstart

This example demonstrates how to define a configuration class using `envier.Env`, specify environment variable mappings with `Env.var`, and handle prefixes for nested configurations. It sets temporary environment variables to ensure the example is runnable.

import os
from envier import Env

# Set environment variables for the example
os.environ['MYAPP_DEBUG'] = os.environ.get('MYAPP_DEBUG', 'True')
os.environ['MYAPP_SERVICE_HOST'] = os.environ.get('MYAPP_SERVICE_HOST', 'example.com')
os.environ['MYAPP_SERVICE_PORT'] = os.environ.get('MYAPP_SERVICE_PORT', '8080')

class GlobalConfig(Env):
    __prefix__ = "myapp"

    debug_mode = Env.var(bool, "debug", default=False)

    service_host = Env.var(str, "service.host", default="localhost")
    service_port = Env.var(int, "service.port", default=3000)

# Instantiate the configuration
config = GlobalConfig()

print(f"Debug Mode: {config.debug_mode}")
print(f"Service Host: {config.service_host}")
print(f"Service Port: {config.service_port}")

# Expected output if env vars are not set:
# Debug Mode: True
# Service Host: example.com
# Service Port: 8080

# Clean up environment variables (optional, but good for isolated testing)
del os.environ['MYAPP_DEBUG']
del os.environ['MYAPP_SERVICE_HOST']
del os.environ['MYAPP_SERVICE_PORT']

view raw JSON →