Search code examples
python-3.xfb-hydra

How to get a Hydra config without using @hydra.main()


Let's say we have following setup (copied & shortened from the Hydra docs):

Configuration file: config.yaml

db:
  driver: mysql
  user: omry
  pass: secret

Python file: my_app.py

import hydra
@hydra.main(config_path="config.yaml")
def my_app(cfg):
    print(cfg.pretty())
if __name__ == "__main__":
    my_app()

This works well when we can use a decorator on the function my_app. Now I would like (for small scripts and testing purposes, but that is not important) to get this cfg object outside of any function, just in a plain python script. From what I understand how decorators work, it should be possible to call

import hydra
cfg = hydra.main(config_path="config.yaml")(lambda x: x)()
print(cfg.pretty())

but then cfg is just None and not the desired configuration object. So it seems that the decorator does not pass on the returned values. Is there another way to get to that cfg ?


Solution

  • Use the Compose API:

    from hydra import compose, initialize
    from omegaconf import OmegaConf
    
    with initialize(version_base=None, config_path="conf", job_name="test_app"):
        cfg = compose(config_name="config", overrides=["db=mysql", "db.user=me"])
    
    print(OmegaConf.to_yaml(cfg))
    

    This will only compose the config and will not have side effects like changing the working directory or configuring the Python logging system.