英文:
Using hydra configuration management with python and yaml file , using notebook in databricks
问题
这是从文档中获取的示例代码:
import hydra
from omegaconf import DictConfig, OmegaConf
@hydra.main(version_base=None, config_path=".", config_name="config")
def my_app(cfg: DictConfig) -> None:
print(OmegaConf.to_yaml(cfg))
if __name__ == "__main__":
my_app()
在我的Databricks工作区中,我有一个名为config.yaml
的文件,位于与此笔记本相同的文件夹中。运行时出现错误 -
OverrideParseException: LexerNoViableAltException: 40473.
我无法理解应该如何在Databricks中运行它。我试图从一个目录中的配置文件中读取配置,使用Hydra完成所有这些工作。
请注意,代码中的HTML实体(如"
)已被保留,但您可以将其替换为双引号(")以获得有效的Python代码。
英文:
This is the sample code obtained from the document.
import hydra
from omegaconf import DictConfig, OmegaConf
@hydra.main(version_base=None, config_path=".", config_name="config")
def my_app(cfg: DictConfig) -> None:
print(OmegaConf.to_yaml(cfg))
if __name__ == "__main__":
my_app()
db:
name:groot
driver: jdbc:mysql
table: peer
user: thanos
password: goldfinger
in my Databricks workspace, I have a file named config.yaml in the same folder where this notebook is present. on running it, gives error -
OverrideParseException: LexerNoViableAltException: 40473.
I am unable to understand what should I do to run it in Databricks.
I am trying to read configs from a config file kept in a directory using Hydra. all this is to be done in Databricks.
答案1
得分: 0
以下是您使用的方法。
下面是config.yaml文件
确保配置文件中的语法正确。
from hydra import compose, initialize_config_dir
from omegaconf import OmegaConf
def main() -> None:
with initialize_config_dir(version_base="1.3", config_dir="/Workspace/Users/path_to_config_folder/"):
cfg = compose(
config_name="config.yaml", return_hydra_config=True, overrides=[]
)
print(cfg.db)
print(OmegaConf.to_yaml(cfg))
if __name__ == "__main__":
main()
在这里,对于config_dir
,请提供包含config.yaml
文件的文件夹的绝对路径。
有关更多信息,请参阅此页面。
英文:
You use below approach.
Below is the config.yaml
Make sure you have correct syntax in config file.
from hydra import compose, initialize_config_dir
from omegaconf import OmegaConf
def main() -> None:
with initialize_config_dir(version_base="1.3", config_dir="/Workspace/Users/path_to_config_folder/"):
cfg = compose(
config_name="config.yaml", return_hydra_config=True, overrides=[]
)
print(cfg.db)
print(OmegaConf.to_yaml(cfg))
if __name__ == "__main__":
main()
Here, for config_dir
give absolute path to the folder containing config.yaml
.
For more info refer this page.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论