Hydra: [Bug] return configuration variable from hydra.main function

Created on 11 Feb 2020  路  7Comments  路  Source: facebookresearch/hydra

馃悰 Hydra return config variable


I try to return the config from a function but get a None return instead. I am trying to keep the config as a global variable for other modules to import the config

To reproduce

import hydra
from omegaconf import DictConfig

from runner import Runner


@hydra.main(config_path="config_hydra/config.yaml")
def my_app(cfg : DictConfig) -> DictConfig:
    # print(cfg.pretty())
    return cfg



if __name__ == "__main__":
    cfg = my_app()
    print(cfg)

* Minimal Code/Config snippet to reproduce *

* Stack trace/error message *

None
// Paste the bad output here!

Expected Behavior

System information

Additional context

Add any other context about the problem here.

bug wontfix

All 7 comments

Hi,
This is not supported.
Your app logic should be inside my_app().

If you just want to generate a config object you can use the compose API, but this will not provide command line integration, automatic working directory, logging support and other things.

So if I have modules that rely on this global config, how would I do that by wrapping all the logic inside the main()? I can't quite figure out how I should use this for more complicated experiments setting, I couldn't just write all the parameters inside a big main() or passing the config as an argument everytime I need it.

I recommend that you do pass the config around (or parts of it).
however, you are free to store is as a global and use it without passing it around (it's just not good practice).


@hydra.main(config_path="config_hydra/config.yaml")
def my_app(cfg : DictConfig) -> DictConfig:
  global config
  config = cfg
  # call the rest of your logic from here.    

if __name__ == "__main__":
    my_app()

As I said, I do recommend that instead of doing this you pass the config or a part of it forward. for example:


def train(cfg):
  ...

def test(cfg):
  ...

@hydra.main(config_path="config_hydra/config.yaml")
def my_app(cfg : DictConfig) -> DictConfig:
  if cfg.action == "train":
    train(cfg)
  elif cfg.action == "test":
    test(cfg)

if __name__ == "__main__":
  my_app()

```

Thank you! That helps.

I encountered this thread because some of the debugger frames were getting lost when there is an error in the function encapsulated by the Hydra decorator. Hence I was trying to get the config in the same way as @noklam did.

It turned out it's already solved in this PR #930

To not lose any debugger stack frames, set HYDRA_FULL_ERROR to 1. That should do the trick.

I hope that helps!

@krishnachaitanya7, I recommend that you always use the latest stable Hydra version :).

@omry definitely! Thank you for the tip!

Was this page helpful?
0 / 5 - 0 ratings

Related issues

moskomule picture moskomule  路  5Comments

Alexfinkelshtein picture Alexfinkelshtein  路  4Comments

omry picture omry  路  5Comments

ImMrMa picture ImMrMa  路  4Comments

vict0rsch picture vict0rsch  路  4Comments