Instantiating an object with hydra from a different folder than the main file

935 Views Asked by At

I have a folder structure looking like this:

  • hydra_configs
    • datasets
      • dataset_config.yaml
    • config.yaml
  • src
    • dataset.py
  • repo
    • main.py

In the main.py, I can set the config_path of the hydra decorator to "../hydra_configs". In the config.yaml I have

datasets: dataset_config

In the dataset_config.yaml I have

_target_: src.KIBA_gluonts_dataset.py

In the main file I want to instatiate my Dataset with

dataset = hydra.utils.instatiate(cfg.datasets)

but when I run it I get the error

Error locating target 'src.KIBA_gluonts_dataset.py'. ModuleNotFoundError: No module named 'src'

How can I instantiate an object from a different folder than the one the main file is located in?

I tried to use a relative path for _target_, but that is not allowed. I also searched for a HydraConf parameter similar to run dir or sweep dir, but to no avail.

1

There are 1 best solutions below

0
Omry Yadan On

Hydra is using Python import logic to find the referenced modules. If you can import your module, you should be able to use the same import string in the config file to reference it.