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.11 or later (and earlier than Python 4).
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", config_dir="config")
# 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 explicit runtime inputs. Pass the selected profile to
load_config(), ConfigLoader, or ConfigSingleton.initialize():
profile = os.environ.get("APP_PROFILE", "dev")
cfg = load_config(profile=profile)
SprigConfig does not select a profile from the environment automatically. Your application owns that policy, which keeps profile selection visible and testable.
The active profile is always available in the config:
print(cfg["app.profile"]) # dev, test, or prod
app.profile is injected from the explicit runtime profile. A value from a
configuration file is overwritten.
Pass the configuration directory explicitly, or set APP_CONFIG_DIR before
loading configuration:
from pathlib import Path
cfg = load_config(profile="dev", config_dir=Path("/opt/myapp/config"))
export APP_CONFIG_DIR=/opt/myapp/config
SprigConfig does not load .env files itself. If your application uses
python-dotenv or another environment loader, invoke it before SprigConfig.
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", config_dir="config")
print(cfg["database.host"]) # db.example.com
print(cfg["database.port"]) # 5432 (default)
Now that you have basic configuration working:
_target_ class instantiation