OmegaConf
OmegaConf is a flexible configuration library (version 2.3.0) that provides hierarchical configuration, variable interpolation, and merging capabilities from multiple sources like YAML files, CLI arguments, and environment variables. It also offers runtime type safety through Structured Configs. The library is actively maintained with frequent releases, including pre-releases for the upcoming 2.4.0 version.
Warnings
- breaking In OmegaConf 2.1, accessing non-existent keys via `DictConfig.__getattr__` (e.g., `cfg.non_existent_key`) now raises an `AttributeError`, and `DictConfig.__getitem__` (e.g., `cfg['non_existent_key']`) raises a `KeyError`. Previously, these operations returned `None`.
- breaking As of OmegaConf 2.1, the `OmegaConf.select()` method requires keyword arguments for all parameters after the `key`. Positional arguments for `default` and `throw_on_missing` are no longer supported.
- gotcha OmegaConf objects (`DictConfig`, `ListConfig`) are not native Python `dict` or `list` instances. Direct usage with APIs expecting standard Python containers can lead to unexpected behavior. They also retain OmegaConf's specific features like interpolation and type safety.
- gotcha In OmegaConf 2.2.1, implicit conversion from `pathlib.Path` objects to `str` was disallowed, leading to `ValidationError` when assigning a `Path` to a string-typed structured config field. This behavior was reverted in versions 2.2.2 and 2.2.3, restoring the implicit conversion.
- gotcha Structured configs with complex type hints (e.g., nested container types, containers with optional element types) cannot be pickled on Python 3.6 due to limitations in Python's pickling support for such types.
- gotcha The `???` syntax indicates a mandatory value in the configuration. Accessing a mandatory value that has not been set (either directly or via interpolation/CLI overrides) will raise a `MissingMandatoryValue` exception at runtime.
Install
-
pip install omegaconf
Imports
- OmegaConf
from omegaconf import OmegaConf
- II
from omegaconf import II
Quickstart
from omegaconf import OmegaConf
import os
# Create a config from a dictionary
conf = OmegaConf.create({
"database": {
"host": "localhost",
"port": 5432,
"user": "${oc.env:DB_USER, default_user}"
},
"server": {
"port": "${database.port}"
}
})
# Access config values
print(f"Database host: {conf.database.host}")
print(f"Database user: {conf.database.user}") # Resolves from env var or default
print(f"Server port: {conf.server.port}")
# You can also load from YAML strings or files
yaml_string = """
app:
name: my_app
version: 1.0
"""
app_conf = OmegaConf.create(yaml_string)
print(f"App name: {app_conf.app.name}")
# Overriding values
conf.database.host = "remote_db"
print(f"Updated database host: {conf.database.host}")
# Example of structured config with dataclasses (requires Python 3.7+ or dataclasses backport for 3.6)
from dataclasses import dataclass
@dataclass
class DatabaseConfig:
host: str = "127.0.0.1"
port: int = 5432
@dataclass
class Config:
db: DatabaseConfig = DatabaseConfig()
debug: bool = False
structured_conf = OmegaConf.structured(Config)
print(f"Structured config DB host: {structured_conf.db.host}")
# Set an environment variable for testing
os.environ['DB_USER'] = 'my_secret_user'
resolved_user_conf = OmegaConf.create({"db_user": "${oc.env:DB_USER}"})
print(f"Resolved DB user from env: {resolved_user_conf.db_user}")
del os.environ['DB_USER'] # Clean up