pythonpython-3.xfb-hydrahydrahydra-core

Access Hydra configuration parameters from different files


I'm trying to integrate hydra into my reinforcement learning code and I would like to access configuration parameters from different files, containing various functions and classes.

Short Example:

Main file main.py:

import hydra

@hydra.main(config_path='config', config_name='config', version_base=None)
def main(cfg):
    print(cfg)
    ENV = cfg.env
    ...

Other file utilities.py:

import hydra

my_hydra_config = ? access to hydra.global loaded configuration parameters
ENV = my_hydra_config.env
...

In utilities.py I have functions that need parameters loaded in the hydra configuration file and I would like to avoid passing them as function parameters but read them directly from hydra.

Is it possible to do ? I'm currently working around this by loading hydra parameters as environmental variables, is there a cleaner way to do this?


Solution

  • The clean way to do it is to pass the configuration object or a sub node (or actual parameters) directly to your modules instead of depending on globals. There are many disadvantages to storing the config in a global:

    1. It makes testing harder
    2. It makes running your code with more than one set of parameters in the same run harder

    There is plenty of good information online about why this is bad (e.g. this).