Skip to main content

Bridge YAML configuration files and environment variables for flexible app deployment

Project description

dotconfig

A Python library that bridges YAML configuration files and environment variables, providing the flexibility to configure applications using either approach.

Installation

pip install dotconfig

For .env file support, also install python-dotenv:

pip install dotconfig python-dotenv

Quick Start

Just like python-dotenv, dotconfig is designed to be simple to use:

from dotconfig import load_config

# Load configuration from YAML file and set environment variables
load_config('config.yaml')

# Now your app can access configuration via environment variables
import os
db_host = os.getenv('APP_DATABASE_HOST')

Basic Usage

1. Create a YAML configuration file

config.yaml:

database:
  host: localhost
  port: 5432
  name: myapp
api:
  timeout: 30
  retries: 3

2. Load configuration in your Python application

from dotconfig import load_config

# This will set environment variables based on your YAML structure
load_config('config.yaml', prefix='APP')

# Environment variables are now available:
# APP_DATABASE_HOST=localhost
# APP_DATABASE_PORT=5432
# APP_DATABASE_NAME=myapp
# APP_API_TIMEOUT=30
# APP_API_RETRIES=3

3. Use environment variables in your application

import os

# Your application code remains simple and flexible
database_config = {
    'host': os.getenv('APP_DATABASE_HOST'),
    'port': int(os.getenv('APP_DATABASE_PORT')),
    'name': os.getenv('APP_DATABASE_NAME')
}

Alternative: Environment Variables Only

Your application works the same way even without a YAML file:

# Set environment variables directly
export APP_DATABASE_HOST=prod-db.example.com
export APP_DATABASE_PORT=5432
export APP_DATABASE_NAME=production
export APP_API_TIMEOUT=60
export APP_API_RETRIES=5
# Your application code doesn't change
import os
database_config = {
    'host': os.getenv('APP_DATABASE_HOST'),
    'port': int(os.getenv('APP_DATABASE_PORT')),
    'name': os.getenv('APP_DATABASE_NAME')
}

Advanced Usage

Environment Variable Precedence

Environment variables always take precedence over YAML values:

# YAML file has database.host: localhost
# But environment variable is set:
os.environ['APP_DATABASE_HOST'] = 'prod-db.example.com'

load_config('config.yaml', prefix='APP')
# Result: APP_DATABASE_HOST=prod-db.example.com (env var wins)

Force Override

Override existing environment variables with YAML values:

load_config('config.yaml', prefix='APP', override=True)

ConfigLoader for Advanced Use Cases

from dotconfig import ConfigLoader

# Load configuration without setting environment variables
loader = ConfigLoader(prefix='APP')
config = loader.load_from_yaml('config.yaml')  # Returns dict

# Load configuration from environment variables only
env_config = loader.load_from_env()

# Set environment variables from configuration dict
loader.set_env_vars(config)

Integration with python-dotenv

dotconfig works perfectly with python-dotenv for .env file support. Since dotconfig respects existing environment variables, you can use both together:

from dotenv import load_dotenv
from dotconfig import load_config

# Load .env file first (if it exists)
load_dotenv()

# Then load YAML config - respects .env values
load_config('config.yaml', prefix='APP')

Precedence order (highest to lowest):

  1. Existing environment variables (including from .env)
  2. YAML configuration file values
  3. Default values (if using schemas)

This pattern gives you maximum flexibility:

  • Development: Use .env for secrets and local overrides
  • Staging: Mix .env and YAML as needed
  • Production: Use environment variables only

Data Type Handling

dotconfig automatically handles various YAML data types:

  • Strings: Passed through as-is
  • Numbers: Converted to string representations
  • Booleans: Converted to "true"/"false"
  • Lists: Converted to comma-separated strings
  • Null values: Converted to empty strings

License

MIT License - see LICENSE file for 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

dotyaml-0.1.0.tar.gz (12.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dotyaml-0.1.0-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file dotyaml-0.1.0.tar.gz.

File metadata

  • Download URL: dotyaml-0.1.0.tar.gz
  • Upload date:
  • Size: 12.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for dotyaml-0.1.0.tar.gz
Algorithm Hash digest
SHA256 126bc8f293fb2aa8d91c8274bbeda6c5770b09415eb281c131fbd222d0c3537e
MD5 41b5ec6ef6e96d7086c5cff503b37677
BLAKE2b-256 307fbf364303e9b58a73aa9206c0f9fe03c0eaadb2d89333ab40f3b36414a9f3

See more details on using hashes here.

File details

Details for the file dotyaml-0.1.0-py3-none-any.whl.

File metadata

  • Download URL: dotyaml-0.1.0-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/6.2.0 CPython/3.11.13

File hashes

Hashes for dotyaml-0.1.0-py3-none-any.whl
Algorithm Hash digest
SHA256 2a806b520dd2589c9e741ee9d97b52a09172286af45315b06eb5c30e900843bd
MD5 d05344ef8ba5a6412a4a9f42a69c3342
BLAKE2b-256 c04f8531de0ad498bf68275602f3f47be8f3ba8d39f8ad662936e3becf6a4a45

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page