[英]Python - Hydra - Obtaining configuration in sub-module
I am currently having trouble with getting hydra to work when not using @hydra.main
in the main.py
script (refer to error section).在
main.py
脚本中不使用@hydra.main
时,我目前无法让 hydra 工作(请参阅错误部分)。 I have tried the compose API, but that prevents me from using most of the functionality that hydra provides.我已经尝试过撰写 API,但这使我无法使用 hydra 提供的大部分功能。 I am unsure what I am trying to achieve is possible.
我不确定我想要实现的目标是否可行。
Project Root
|
├── config
│ ├── default.yaml
│ ├── configuration1.yaml
│ └── configuration2.yaml
└── src
├── utils
│ └── initialise.py
└── main.py
initialise.py初始化.py
import hydra
import omegaconf
CONFIG = None
@hydra.main(version_base=None, config_path='../../config', config_name='default')
def init_configs(cfg: omegaconf.DictConfig) -> None:
global CONFIG
CONFIG = cfg
# Other initialisation code here.
init_configs()
main.py主文件
from utils import initialise
print(initialise.CONFIG)
Primary config module 'config' not found.
Check that it's correct and contains an __init__.py file
This is an attempt to implement an many anti-patterns.这是实现许多反模式的尝试。
@hydra.main()
is designed to be your entry point. @hydra.main()
旨在成为您的入口点。 Using it a utility function is wrong.使用它的实用程序 function 是错误的。
key: ${foo.bar}
).key: ${foo.bar}
)。@hydra.main()
entry points as you have runnable modules@hydra.main()
入口点If you insist on having a config initialization utility, you need to let go of @hydra.main()
and use the compose API.如果您坚持使用配置初始化实用程序,则需要让
@hydra.main()
的 go 并使用 compose API。 You can also combine the two but my recommendation to just let hydra.main() compose your config.您也可以将两者结合起来,但我的建议是让 hydra.main() 组成您的配置。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.