Calling a hydra app from a module rather than from the CLI

86 views Asked by At

Usually to run a hydra app I would compose my app as follows:

my_package
|
├── config
│   ├── config.yaml
└── main.py

My main.py file might look like:

import hydra    
@hydra.main(version_base=None, config_path="conf", config_name="config")
def main(cfg: DictConfig):
    print('hello!')
    
if __name__ == '__main__':
    main()

to run it I simple run

python main.py in the CL in the relevant directory

This works great!

I have now been asked to productionise my code and port it to a docker container. Which comes with additional constraints I won't go into here, as such I have had to rebuild my code as a package. Now trying to run my app:

import my_package
import os
from importlib.resources import files
from omegaconf import OmegaConf

# load in my config from the non-code files included in the setup.py manifest
# as they are not allowed to exist on the docker container
cfg = OmegaConf.load(os.path.join(files("my_package"), 'conf', 'config.yaml'))

# start hydra app
my_package.main(cfg)

This does not work, and I am greeted with an UnsupportedInterpolationType: Unsupported interpolation type hydra error as I guess the config I supplied was not suitable.

I am aware of the compose API, which allows users to create their own configs independent of the @hydra.main process, however I am not trying to compose an config, I already have one, I simply want to be able to run the app without calling it from the CL.

Is there anyway to do this?

Happy to clarify anything that isn't clear

Helpful related questions:

Referring to Hydra's conf directory from a Python sub/sub-sub-directory module

How to load Hydra parameters from previous jobs (without having to use argparse and the compose API)?

0

There are 0 answers