Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Is there a way to get the current file location within the config file? #3028

Open
S-Dafarra opened this issue Feb 14, 2025 · 1 comment
Open

Comments

@S-Dafarra
Copy link

Hi there, great work! I am new to hydra and am looking for a way to have the absolute path to the configuration file itself in the parameters.

Suppose to have a tree structure like the following

.
└── config/
    ├── config.yaml
    └── module/
        ├── module_1.yaml
        └── other_config_file.ini

With config.yaml being

defaults:
  - module: module_1

Within module_1.yaml I would like to have something like

this_file_dir: ${the_variable_I_would_need}
ini_file: ${this_file_dir}/other_config_file.ini

In this way, in code, I could do something like

    @hydra.main(config_path="./config", config_name="config", version_base="1.1")
    def main(cfg: DictConfig):
        f = third_party_library_needing_absolute_path.open(cfg.module.ini_file)

From #1680 I see it is possible to get the current working directory, but from https://hydra.cc/docs/0.11/configure_hydra/intro/#runtime-variables, this variable indicates the "Original working directory the app was executed from". I thought I could assume that

the_variable_I_would_need = ${hydra:runtime.cwd}/config/module

but by reading https://hydra.cc/docs/advanced/search_path/ it is possible to override the search path from a configuration parameter. Hence, in principle, I could run the script from any folder and then specify the search path, but this would break the assumption that my config folder is a subfolder of cwd.

So, how can I get the resolved path to the module_1.yaml file as a configuration parameter?

@IINemo
Copy link

IINemo commented Feb 28, 2025

I think the big problem of Hydra is that it does not even allow to get the config path from the code itself. You can get the config name and a list of sources, one of which was used to load this config file. I would suggest adding

  1. Ability to get the loaded config path from the python code (e.g. populate the runtime)
  2. Ability to make relative paths from the config file itself.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants