0

I have a complex config search path consisting of multiple locations where each location looks similar to this:

├── conf
│   └── foo
│       ├── foo.yaml
│       └── bar.yaml
└── files
    ├── foo.txt
    └── bar.txt

with foo.yaml:

# @package _group_
path: "../../files/foo.txt"

and bar.yaml:

# @package _group_
path: "../../files/bar.txt"

Now the problem is: how do I find the correct location of the files specified in the configurations? I am aware of the to_absolute_path() method provided by hydra, but it interprets the path relative to the directory in which the application was started. However, I would like to interpret that path relative to the position of the configuration file. I cannot do this manually in my code, because I don't know how hydra resolved the configuration file and where exactly it is used to.

Is there some mechanism to determine the location of a config file from hydra? I really want to refrain from putting hard coded absolute paths in my configurations.

1 Answers1

0

You can't get the path of a config file. In fact, it may not be a file at all (such as the case for Structured Configs), or it can be inside a python wheel (even in a zipped wheel).

You can do something like

path = os.path.join(os.path.dirname(__file__), "relative_path_from_config")

You can use also APIs designed for loading resources files from Python modules. Here is a good answer in the topic.

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
  • Right, my question was probably misstated in that regard. However, whether the configs and resources are actual files or not, I need to identify the correct base for any relative paths specified in the configs (or the package to extract files from). Having multiple plugins, it is not obvious what the base of a specified path is. Hard coding absolute paths is no option. I managed to solve my problem with custom OmegaConf resolvers (`"${plugin_name:"`)}, but I was hoping for a built-in solution. – Christian Menard Feb 02 '21 at 15:53
  • SO is not a place for feature requests. This is the first time I hear someone asking for this so you can be sure there is not going to be a built in solution. Feel free to file a proper feature request (starting with what problem you are trying to solve). – Omry Yadan Feb 03 '21 at 04:59