Bridge YAML configuration files and environment variables for flexible app deployment
Project description
dotyaml
A Python library that bridges YAML configuration files and environment variables, providing the flexibility to configure applications using either approach.
Installation
pip install dotyaml
For .env file support, also install python-dotenv:
pip install dotyaml python-dotenv
Quick Start
Just like python-dotenv, dotyaml is designed to be simple to use:
from dotyaml 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 dotyaml 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 dotyaml 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
dotyaml 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 dotyaml 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):
- Existing environment variables (including from
.env) - YAML configuration file values
- Default values (if using schemas)
This pattern gives you maximum flexibility:
- Development: Use
.envfor secrets and local overrides - Staging: Mix
.envand YAML as needed - Production: Use environment variables only
Data Type Handling
dotyaml 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
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file dotyaml-0.1.1.tar.gz.
File metadata
- Download URL: dotyaml-0.1.1.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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
55bb69445d6e7bf5bbc3c9a01d79e374e1d9249e246d3c1b9d580302ba386a24
|
|
| MD5 |
c76969321b244795b8583bfbde70ddad
|
|
| BLAKE2b-256 |
4e9d19ba5e7d41b663a3a040165b1a9b925d366409f1276cbd61115baa3bce4b
|
File details
Details for the file dotyaml-0.1.1-py3-none-any.whl.
File metadata
- Download URL: dotyaml-0.1.1-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
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
957f0eed29680b6195b65390695a5e66aef0ba39bb4f3c2c0abb99ef2c2c4cfe
|
|
| MD5 |
faecb7fb6624984a4ccb527d3eac468d
|
|
| BLAKE2b-256 |
824802aabe3f30cf08d10f7fbd257887e1eeb4e769a38e33086b4461e1003585
|