Search code examples
python-3.xconfigfb-hydra

overriding the package specified in default list from cli


I am trying to override the package in default list in a hierarchical configuration structure. As a simplified example:

I have conf/base.yaml

defaults:
  - _self_
  - env@_here_: env1
a: 1
b: 2

conf/env/env1.yaml

c: 5
d: 6

and conf/env/env2.yaml

c: 7
d: 8

When running my_app.py

import hydra
import omegaconf

@hydra.main(config_path="conf", config_name="base")
def my_app(cfg: omegaconf.DictConf) -> None:
    print(omegaconf.OmegaConf.to_yaml(cfg))

my_app()

I want to override env@_here_ from env1 to env2 using the CLI or any other method. I have gone through Hydra documentation a few times, but couldn't find how to do this.


Solution

  • In the current hydra release (Hydra 1.1.1) you can use:

    python my_app.py env=env2
    

    Due to a recent breaking change to improve consistency of Hydra's API, it will soon be necessary to do this:

    python my_app.py env@_global_=env2
    

    This breaking change will probably be a part of Hydra 1.1.2 or 1.2.0.

    Side note: There are a few different ways to achieve the same result as you have in your example above. Your options include:

    • Using - env@_here_: env1 in base.yaml, which is what you did above.
    • Using - env@_global_: env1 in base.yaml
    • Using - env: env1 in base.yaml and adding a _global_ package header to env1.yaml and env2.yaml

    conf/env/env1.yaml:

    # @package _global_
    c: 5
    d: 6
    

    conf/env/env2.yaml:

    # @package _global_
    c: 7
    d: 8
    

    The keyword _global_ refers to the top-level package in the output config produced by Hydra. You can read more here about the _global_ and _here_ keywords.