Spring Boot-style configuration for Python applications
This guide walks you through installing SprigConfig and loading your first configuration.
Install SprigConfig from PyPI:
pip install sprig-config
Or with Poetry:
poetry add sprig-config
Requirements: Python 3.13 or later.
Create a config/ directory in your project root:
my-project/
├── config/
│ ├── application.yml
│ ├── application-dev.yml
│ ├── application-test.yml
│ └── application-prod.yml
├── src/
│ └── ...
└── pyproject.toml
Create config/application.yml with shared defaults:
# config/application.yml
server:
host: localhost
port: 8080
database:
pool_size: 5
timeout: 30
logging:
level: INFO
format: "%(levelname)s - %(message)s"
Create config/application-dev.yml for development overrides:
# config/application-dev.yml
server:
port: 9090
logging:
level: DEBUG
Create config/application-prod.yml for production:
# config/application-prod.yml
server:
host: 0.0.0.0
database:
pool_size: 20
logging:
level: INFO
from sprigconfig import load_config
# Load with explicit profile
cfg = load_config(profile="dev")
# Access values
print(cfg["server"]["port"]) # 9090
print(cfg["server"]["host"]) # localhost
print(cfg["database"]["pool_size"]) # 5
SprigConfig supports convenient dotted-key notation:
# These are equivalent
port = cfg["server"]["port"]
port = cfg["server.port"]
port = cfg.get("server.port")
# With default value
port = cfg.get("server.port", 8080)
if "server.port" in cfg:
print("Port is configured")
Profiles are determined at runtime in this order:
load_config(profile="prod")APP_PROFILE=prod"test""dev"The active profile is always available in the config:
print(cfg["app.profile"]) # dev, test, or prod
Important: Never set app.profile in your YAML files. SprigConfig ignores it with a warning.
By default, SprigConfig looks for config/ in your project root. You can customize this:
from pathlib import Path
cfg = load_config(profile="dev", config_dir=Path("/opt/myapp/config"))
export APP_CONFIG_DIR=/opt/myapp/config
Create a .env file in your project root:
APP_CONFIG_DIR=/opt/myapp/config
APP_PROFILE=dev
For more control, use the ConfigLoader class directly:
from pathlib import Path
from sprigconfig import ConfigLoader
loader = ConfigLoader(
config_dir=Path("config"),
profile="dev"
)
cfg = loader.load()
For applications that need global access to configuration:
from sprigconfig import ConfigSingleton
# Initialize once at startup
ConfigSingleton.initialize(profile="prod", config_dir="config")
# Access from anywhere in your application
cfg = ConfigSingleton.get()
print(cfg["database.pool_size"])
Note: initialize() can only be called once. Subsequent calls raise an error.
Configuration values can reference environment variables:
# config/application.yml
database:
host: ${DB_HOST}
port: ${DB_PORT:5432} # Default to 5432 if not set
password: ${DB_PASSWORD}
export DB_HOST=db.example.com
export DB_PASSWORD=secret
cfg = load_config(profile="prod")
print(cfg["database.host"]) # db.example.com
print(cfg["database.port"]) # 5432 (default)
Now that you have basic configuration working:
ConfigValue and @ConfigurationProperties_target_ class instantiation