Hydra uses YAML-like syntax for configuration files, typically with a .yaml
extension.
key: value
nested:
key: value
list:
- item1
- item2
- Composition: Combine multiple config files
- Command-line overrides: Override config values from CLI
- Config groups: Organize related configs
- Interpolation: Reference other config values
defaults: # reserved keyword
- base_config
- environment: production
- _self_
database:
host: localhost
port: 5432
app:
_target_: my_package.App
name: MyApp
version: 1.0
_target_
: target can be both class and function.
import hydra
from omegaconf import DictConfig
@hydra.main(version_base=None, config_path="conf", config_name="config")
def my_app(cfg: DictConfig) -> None:
print(cfg.database.host)
print(cfg.app.name)
if __name__ == "__main__":
my_app()
python my_app.py database.host=127.0.0.1 app.name=NewName
Organize related configs in directories:
conf/
config.yaml
database/
mysql.yaml
postgresql.yaml
environment/
development.yaml
production.yaml
Reference other config values:
app:
name: MyApp
log_file: ${app.name}.log
Run with multiple configurations:
python my_app.py --multirun database=mysql,postgresql
- Sweeping: Systematically explore config variations
- Plugins: Extend Hydra's functionality
- Custom resolvers: Define custom interpolation functions
Remember, Hydra is highly flexible and offers many more features for complex configuration management in Python applications[1][3][4].
Citations: [1] https://hydra.cc/docs/intro/ [2] https://python.plainenglish.io/managing-configuration-in-python-with-hydra-a-beginners-guide-cedebf9533ca?gi=ea764b7c7752 [3] https://hydra.cc/docs/tutorials/basic/your_first_app/config_file/ [4] https://hydra.cc/docs/configure_hydra/intro/