Call system environment in .yaml file when using hydra core python package

26 views Asked by At

I want to call system environment in .yaml like below (error occur):

system_env1:   ${SYS_ENV_1}

I have tried to use the code, but it doesn't work. I don't want to call the env in python code, and it should be called in .yaml file. I also tried (it doesn't work what I expect):

system_env1:   $SYS_ENV_1
1

There are 1 answers

0
Anthon On

You should probably read the YAML 1.2 specification, or at least try to dump the Python string "$abc" to a YAML document file, and inspect the output. You'll see that nothing special, like escaping, is necessary for the $. You stating "it should be called in .yaml file" is therefore misguided, a standard conforming parser will just load the $ as is, even if it occurs at the beginning of a scalar.

You (or hydra-core) need to postprocess loaded data and replace string values starting with the pattern which means doing things in Python. Or you can preprocess the text with some template expander before it is handed to the YAML parser. The later option is in my experience more often done, but in that case you should, IMO, not call the input a YAML file and not use the .yaml extension, as that obviously causes confusion.