Suppose that we have a Python project with this structure:
hydra_config
├── conf
│ ├── api_key
│ │ ├── non_prod.yaml
│ │ └── prod.yaml
│ └── db
│ ├── mysql.yaml
│ └── postgresql.yaml
├── modules
│ └── module.py
└── my_app.py
Now, in Hydra's config documentation, they state that we need to add a Python decorator on top of a function which we want to give access to the configuration files. However, the docs only showed how to do this to a function in my_app.py
which is the main module of the project.
The question is, how would one add the
@hydra.main(config_path="conf")
Python decorator to a function, let's say module_function
which is located in modules/module.py
? Here's the content of module.py
:
import hydra
from omegaconf import DictConfig, OmegaConf
@hydra.main(config_path="conf")
def module_function(cfg: DictConfig):
print(OmegaConf.to_yaml(cfg))
And then below is the content of the main Python module my_app.py
:
from modules.module import module_function
def main():
module_function()
if __name__ == "__main__":
main()
When I tried running the main Python module my_app.py
with python my_app.py
, I instantly got an error saying
Primary config module 'modules.conf' not found.
Check that it's correct and contains an __init__.py file
Set the environment variable HYDRA_FULL_ERROR=1 for a complete stack trace.
I understand that this means the decorator added to module_function
inside module.py
couldn't find the conf
directory which contains api_key and db config groups.
Does anyone here have any experience with this and know how to fix this error?