Hello Team, I have a txt file that contains enviro...
# questions
s
Hello Team, I have a txt file that contains environment variables and db credentials in it in key value form. Is there a way that I can load the db_credentials in catalog from this file, as well as populate the globals.yml values from the values found in the txt file. Any help or suggestions would be great.
v
@Sneha Kumari I used a .env file(to store all the k-v pairs) and used python-dotenv to load all the key-value pairs from .env to Env variables. Once they are set as env variables We can make use of resolvers as mentioned in the above link .
l
Copy code
"""Custom resolvers for Kedro project."""

import os
from typing import Dict, Optional
from copy import deepcopy

from dotenv import load_dotenv

load_dotenv()


def env(key: str, default: str = None) -> Optional[str]:
    """Load a variable from the environment.

    See <https://omegaconf.readthedocs.io/en/latest/custom_resolvers.html#custom-resolvers>

    Args:
        key (str): Key to load.
        default (str): Default value to use instead

    Returns:
        str: Value of the key
    """
    try:
        value = os.environ.get(key, default)
        if value is None:
            raise KeyError()
        return value
    except KeyError:
        raise KeyError(f"Environment variable '{key}' not found or default value {default} is None")
v
@Laurens Vijnck Thanks for the above implementation. But If I am not wrong oc.env from OmegaConf does the same job right ? https://omegaconf.readthedocs.io/en/latest/custom_resolvers.html#built-in-resolvers
l
I'm not sure if it picks up entries from a .env file
But good point, think you could also load the dotenv in the settings file from Kedro
v
yeaaah So I also have used dotenv in the settings.py and oc.env works like charm .
👍 1
l
Cheers, let me update that in that case as well.