Skip to content

Instantly share code, notes, and snippets.

@bdsaglam
Last active December 31, 2024 09:00
Show Gist options
  • Save bdsaglam/586704a98336a0cf0a65a6e7c247d248 to your computer and use it in GitHub Desktop.
Save bdsaglam/586704a98336a0cf0a65a6e7c247d248 to your computer and use it in GitHub Desktop.
Use Hydra in Jupyter notebook
import pathlib
import hydra
hydra._internal.hydra.GlobalHydra().clear()
config_dir = pathlib.Path('/path/to/configs/')
hydra.experimental.initialize(config_dir=config_dir)
cfg = hydra.experimental.compose(config_file='config.yaml', overrides=[])
print(cfg.pretty())
@bruce-willis
Copy link

bruce-willis commented Feb 21, 2023

for new hydra versions (definitely for >1.2, maybe earlier too) use
hydra.core.global_hydra.GlobalHydra.instance().clear() instead of hydra._internal.hydra.GlobalHydra().clear().

Also, hydra does not contain experimental anymore (and cfg.pretty also does not exist) and config_file was renamed to config_name.

So, to sum up:

from hydra import initialize, compose
from omegaconf import OmegaConf

with initialize(version_base=None, config_path="conf/"):
    cfg = compose(config_name='config.yaml')
    print(OmegaConf.to_yaml(cfg))

Source: hydra compose_configs_in_notebook

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment