首页 > 解决方案 > 使用未与已安装的 Hydra 应用程序打包的配置

问题描述

在打包 Hydra 应用程序并包含一个脚本作为入口点时,可以在任何地方从命令行调用该脚本。调用该脚本时,将使用包中包含的配置。有没有办法修改配置搜索路径,以便可以将不同的配置传递给脚本?

例如:

应用程序.py

import hydra


@hydra.main(config_path="conf", config_name="config")
def my_func(cfg):
    print(cfg)

配置.yaml

key: value
another_key: second_value

使用entry_points={"console_scripts": ["hydra_app_test = hydra_app_test.app:my_func"]}.

$ hydra_app_test
{'key': 'value', 'another_key': 'second_value'}

我希望能够定义一个本地配置并将其传递给hydra_test_app. 这样的事情可能吗?

一些/config/path/config.yaml

key: not_value
mon: key
$ hydra_app_test --config-override=some/config/path
{'key': 'not_value', 'mon': 'key'}

我曾尝试使用--config-dirand--config-path覆盖,但没有任何运气。

$ hydra_app_test --config-dir=some/config/path
{'key': 'value', 'another_key': 'second_value'}
$ hydra_app_test --config-path=some/config/path
Primary config module 'hydra_app_test.some.config.path' not found.
Check that it's correct and contains an __init__.py file

有趣的是,如果您不使用已安装的应用程序,但将 app.py 作为脚本运行(具有必要的if __name__ == "__main__"逻辑) ,则此模式有效

python path/to/app.py --config-path=some/config/path
{'key': 'not_value', 'mon': 'key'}

也许我只是在这里遗漏了一些东西,但看起来你应该能够为已安装的包脚本和 python 脚本复制相同的行为。

标签: pythonfb-hydra

解决方案


推荐阅读