37

Let's say we have following setup (copied & shortened from the Hydra docs):

Configuration file: config.yaml

db:
  driver: mysql
  user: omry
  pass: secret

Python file: my_app.py

import hydra
@hydra.main(config_path="config.yaml")
def my_app(cfg):
    print(cfg.pretty())
if __name__ == "__main__":
    my_app()

This works well when we can use a decorator on the function my_app. Now I would like (for small scripts and testing purposes, but that is not important) to get this cfg object outside of any function, just in a plain python script. From what I understand how decorators work, it should be possible to call

import hydra
cfg = hydra.main(config_path="config.yaml")(lambda x: x)()
print(cfg.pretty())

but then cfg is just None and not the desired configuration object. So it seems that the decorator does not pass on the returned values. Is there another way to get to that cfg ?

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
flawr
  • 10,814
  • 3
  • 41
  • 71

5 Answers5

53

Use the Compose API:

from hydra import compose, initialize
from omegaconf import OmegaConf

initialize(config_path="conf", job_name="test_app")
cfg = compose(config_name="config", overrides=["db=mysql", "db.user=me"])
print(OmegaConf.to_yaml(cfg))

This will only compose the config and will not have side effects like changing the working directory or configuring the Python logging system.

Omry Yadan
  • 31,280
  • 18
  • 64
  • 87
  • @OmryYadan Is this possible to update the solution. I find signatures have changed. And is it possible to construct a cfg without refer to some "config.yml"? – Qinsheng Zhang Nov 27 '20 at 02:08
  • Updated, check the API link for more info. (Yes, you can omit the config name). – Omry Yadan Nov 28 '20 at 08:34
  • Hi, @OmryYadan thanks for your response! I'm trying to use this approach and am getting this error. Was there a change in the way to import compose? `hydra-core==1.1.0.dev5` `ImportError: cannot import name 'compose' from 'hydra' ` – aksg87 Aug 17 '21 at 16:27
  • Figured it out. We need to use `hydra.experimental` If this is correct perhaps we can update the solution above! – aksg87 Aug 17 '21 at 16:47
13

None of the above solutions worked for me. They gave errors:

'builtin_function_or_method' object has no attribute 'code'

and

GlobalHydra is already initialized, call Globalhydra.instance().clear() if you want to re-initialize

I dug further into hydra and realised I could just use OmegaConf to load the file directly. You don't get overrides but I'm not fussed about this.

import omegaconf
cfg = omegaconf.OmegaConf.load(path)
forgetso
  • 2,194
  • 14
  • 33
  • 2
    This answer works for regular config files but lets say if you have different structures and paths/dirs/configs for stage/production/development configuration then you have to have these in one file which is tedious to maintain Hydra resolves this. Else if you have only one config then go for this better and faster solution. – ASHu2 Jul 20 '21 at 07:08
1

I found a rather ugly answer but it works - if anyone finds a more elegant solution please let us know!

We can use a closure or some mutable object. In this example we define a list outside and append the config object:

For hydra >= 1.0.0 you have to use config_name instead, see documentation.

import hydra
c = []
hydra.main(config_name="config.yaml")(lambda x:c.append(x))()
cfg = c[0]
print(cfg)

For older versions:

import hydra
c = []
hydra.main(config_path="config.yaml")(c.append)()
cfg = c[0]
print(cfg.pretty())
flawr
  • 10,814
  • 3
  • 41
  • 71
  • 3
    I haven't seen this one before, creative :) – Omry Yadan Apr 12 '20 at 10:05
  • @hahnec You have to use `config_name` now instead of config_path`, check out the [change log](https://hydra.cc/docs/next/upgrades/0.11_to_1.0/config_path_changes/)! – flawr May 04 '22 at 08:42
0

anther ugly answer, but author said this may be crush in next version

Blockquote

from omegaconf import DictConfig
from hydra.utils import instantiate
from hydra._internal.utils import _strict_mode_strategy, split_config_path, create_automatic_config_search_path
from hydra._internal.hydra import Hydra
from hydra.utils import get_class 

class SomeThing:
...
def load_from_yaml(self, config_path, strict=True):
    config_dir, config_file = split_config_path(config_path)
    strict = _strict_mode_strategy(strict, config_file)
    search_path = create_automatic_config_search_path(
        config_file, None, config_dir
    )
    hydra = Hydra.create_main_hydra2(
        task_name='sdfs', config_search_path=search_path, strict=strict
    )
    config = hydra.compose_config(config_file, [])
    config.pop('hydra')
    self.config = config
    print(self.config.pretty())
  • Any time you use anything in an _internal package, know that there will be no attempts to maintain backward compatibility or document changes. Use the compose API, it's simpler to use to is maintained like an API. Although it's still experimental it's much more stable than internal things. – Omry Yadan May 29 '20 at 02:56
0

This is my solution

from omegaconf import OmegaConf

class MakeObj(object):
    """ dictionary to object. 
    Thanks to https://stackoverflow.com/questions/1305532/convert-nested-python-dict-to-object

    Args:
        object ([type]): [description]
    """
    def __init__(self, d):
        for a, b in d.items():
            if isinstance(b, (list, tuple)):
                setattr(self, a, [MakeObj(x) if isinstance(x, dict) else x for x in b])
            else:
                setattr(self, a, MakeObj(b) if isinstance(b, dict) else b)


def read_yaml(path):
    x_dict =  OmegaConf.load(path)
    x_yamlstr = OmegaConf.to_yaml(x_dict)
    x_obj = MakeObj(x_dict)
    return x_yamlstr, x_dict, x_obj
    
x_yamlstr, x_dict, x_obj = read_yaml('config/train.yaml')
print(x_yamlstr)
print(x_dict)
print(x_obj)
print(dir(x_obj))
               
Maulik Madhavi
  • 156
  • 1
  • 3