Skip to main content
Version: 1.1

Grouping config files

ย Example

Suppose you want to benchmark your application on each of PostgreSQL and MySQL. To do this, use config groups.

A Config Group is a named group with a set of valid options. Selecting a non-existent config option generates an error message with the valid options.

Creating config groups#

To create a config group, create a directory. e.g. db to hold a file for each database configuration option. Since we are expecting to have multiple config groups, we will proactively move all the configuration files into a conf directory.

Directory layout
โ”œโ”€ confโ”‚  โ””โ”€ dbโ”‚      โ”œโ”€ mysql.yamlโ”‚      โ””โ”€ postgresql.yamlโ””โ”€โ”€ my_app.py
db/mysql.yaml
driver: mysqluser: omrypassword: secret

db/postgresql.yaml
driver: postgresqluser: postgres_userpassword: drowssaptimeout: 10

Using config groups#

Since we moved all the configs into the conf directory, we need to tell Hydra where to find them using the config_path parameter. config_path is a directory relative to my_app.py.

my_app.py
@hydra.main(config_path="conf")def my_app(cfg: DictConfig) -> None:    print(OmegaConf.to_yaml(cfg))

Running my_app.py without requesting a configuration will print an empty config.

$ python my_app.py{}

Select an item from a config group with +GROUP=OPTION, e.g:

$ python my_app.py +db=postgresqldb:  driver: postgresql  pass: drowssap  timeout: 10  user: postgres_user

By default, the config group determines where the config content is placed inside the final config object. In Hydra, the path to the config content is referred to as the config package. The package of db/postgresql.yaml is db:

Like before, you can still override individual values in the resulting config:

$ python my_app.py +db=postgresql db.timeout=20db:  driver: postgresql  pass: drowssap  timeout: 20  user: postgres_user

Advanced topics#