Is it possible to specify paths in a configuration file that are relative to the configuration file location?

3k Views Asked by At

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

There are 1 best solutions below

2
Omry Yadan On

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.