question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. ItĀ collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

`InterpolationKeyError`: Cannot use basic variable interpolation

See original GitHub issue

šŸ› Bug

Description

Running into an issue trying to use variable interpolation in the most basic case.

Checklist

  • I checked on the latest version of Hydra
  • I created a minimal repro (See this for tips).

To reproduce

### dataset.yaml ###
dataset:
    cwd: ${hydra:runtime.cwd}
    data_dir: ../${cwd}/data
### From testing_configs.py ###
print(cfg['dataset'].data_dir)

which produces the following:

...
raise InterpolationKeyError(f"Interpolation key '{inter_key}' not found")
omegaconf.errors.InterpolationKeyError: Interpolation key 'cwd' not found

Expected Behavior

I expect the path to be correctly printed:

/path/to/cwd/data/

System information

  • Hydra Version : 1.2.0 / omegaconf==2.2.3
  • Python version : 3.8.13
  • Virtual environment type and version : conda (used pip install hydra-core —upgrade)
  • Operating system : macOS 12.6

Additional context

I’m using a dataclass spec file with a ConfigStore instance.

Side note:

Why do I need the import statement from folder.file import Dataset in testing_configs.py file? I get a NameError otherwise.

Issue Analytics

  • State:closed
  • Created a year ago
  • Comments:6 (4 by maintainers)

github_iconTop GitHub Comments

1reaction
Jasha10commented, Oct 8, 2022

You’ll probably need ${.cwd}/../.. to get two levels up, not ../${.cwd}.

If ${.cwd} is /foo/bar/baz then ${.cwd}/../.. will be equivalent to /foo/bar/baz/../.. while ../${.cwd} is equivalent to ..//foo/bar/baz (which is probably not what you want).

import os

from omegaconf import OmegaConf

OmegaConf.register_new_resolver(
    "abspath", lambda relative_path: os.path.abspath(relative_path)
)

yaml_data = """
### dataset.yaml ###
dataset:
    cwd: /foo/bar/baz
    data_dir_relative: ${.cwd}/../../data
    data_dir_absolute: ${abspath:${.cwd}/../../data}
"""

cfg = OmegaConf.create(yaml_data)
assert cfg.dataset.data_dir_relative == "/foo/bar/baz/../../data"
assert cfg.dataset.data_dir_absolute == "/foo/data"
0reactions
jonathanloganmorancommented, Oct 8, 2022

That’s awesome! Thanks so much for helping me understand this — everything works perfectly!

Read more comments on GitHub >

github_iconTop Results From Across the Web

Interpolation in Hydra's defaults list cause and error
Interpolation Keys in the Defaults List cannot reference values in the Final Config Object (it does not yet exist). In particular, it isĀ ......
Read more >
dir interpolation from hydra into job config is not working #240
KeyError : "str interpolation key 'experiment.base_dir' not found". Is there any way I can change the hydra config based on current .yaml orĀ ......
Read more >
Python String Interpolation - Towards Data Science
String interpolation is a process of injecting value into a placeholder (a placeholder is nothing but a variable to which you can assign...
Read more >
omegaconf.errors.interpolationresolutionerror: valueerror ...
Hi I am wondering if it is possible to use hydra: namespace in the sweep. ... The problem is that I can't resolve...
Read more >
PEP 498 – Literal String Interpolation
F-strings provide a way to embed expressions inside string literals, using a minimal syntax. It should be noted that an f-string is really...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found