A framework for elegantly configuring complex applications
—
The @hydra.main decorator is the primary entry point for Hydra-enabled applications. It transforms regular Python functions into configuration-aware applications with automatic command-line interface generation and configuration injection.
Decorator that enables Hydra configuration management for application functions. When applied to a function, it automatically handles configuration loading, command-line argument parsing, and dependency injection.
def main(
config_path: Optional[str] = None,
config_name: Optional[str] = None,
version_base: Optional[str] = None
) -> Callable[[TaskFunction], Any]:
"""
Decorator for Hydra-enabled applications.
Parameters:
- config_path: Directory where Hydra searches for config files.
Relative paths are interpreted relative to the declaring python file.
Can use "pkg://" prefix to specify a python package.
If None, no directory is added to the config search path.
- config_name: Name of the config file (usually without .yaml extension)
- version_base: Hydra version compatibility base for behavior control
Returns:
Decorator function that wraps TaskFunction
"""Basic usage with file-based configuration:
@hydra.main(version_base=None, config_path="conf", config_name="config")
def my_app(cfg: DictConfig) -> None:
print(cfg.db.driver)
print(cfg.db.user)Usage without configuration files:
@hydra.main(version_base=None, config_path=None)
def my_app(cfg: DictConfig) -> None:
# cfg will be empty DictConfig
print("Running with no config")Usage with package-based config path:
@hydra.main(version_base=None, config_path="pkg://my_package.conf", config_name="config")
def my_app(cfg: DictConfig) -> None:
print(cfg)Command-line override examples:
# Override configuration values
python my_app.py db.driver=postgresql db.port=5432
# Run in multirun mode
python my_app.py --multirun db.driver=mysql,postgresql
# Change config file
python my_app.py --config-name=productionThe config_path parameter supports several patterns:
pkg://package.name to reference Python package resourcesThe version_base parameter controls backward compatibility behavior:
The decorated function must accept a single parameter that is compatible with DictConfig:
# Correct signatures
def my_app(cfg: DictConfig) -> None: ...
def my_app(cfg: Any) -> None: ...
def my_app(cfg) -> None: ...
# Incorrect - multiple parameters not supported
def my_app(cfg: DictConfig, other_param: str) -> None: ... # Wrong!The decorated function can return any value. In multirun mode, return values from all jobs are collected and can be accessed through job return objects in callbacks or launchers.
The main decorator works seamlessly with structured configurations stored in ConfigStore:
from dataclasses import dataclass
from hydra.core.config_store import ConfigStore
@dataclass
class Config:
name: str
value: int
cs = ConfigStore.instance()
cs.store(name="config", node=Config)
@hydra.main(version_base=None, config_path=None, config_name="config")
def my_app(cfg: Config) -> None: # Type hint matches stored config
print(f"Name: {cfg.name}, Value: {cfg.value}")Install with Tessl CLI
npx tessl i tessl/pypi-hydra-core