Skip to content

read_creds_from_dotenv

read_creds_from_dotenv

Environment Credentials Reading Utilities

This module provides utilities for reading credentials and configuration from environment files (.env) with proper error handling and validation.

Functions:

Name Description
read_creds_from_dotenv

Read credentials from .env file and return as DictDot object

Exception Classes

CredentialsError: Raised when credential reading fails

Example

Read all environment variables

creds = read_creds_from_dotenv(".env") print(creds.DATABASE_URL)

Read specific parameters

creds = read_creds_from_dotenv(".env", ["API_KEY", "SECRET_KEY"]) print(creds.API_KEY)

Handle missing file gracefully

try: ... creds = read_creds_from_dotenv("missing.env") ... except CredentialsError as e: ... print(f"Error: {e}")

read_creds_from_dotenv

read_creds_from_dotenv(
    env_path: str = ".env", params: list[str] | None = None
) -> DictDot

Read credentials from .env file and return as DictDot object for easy access.

Loads environment variables from the specified .env file and returns them as a DictDot object for convenient dot notation access.

Parameters:

Name Type Description Default
env_path str

Path to the .env file (default: ".env")

'.env'
params list[str]

list of specific parameters to extract. If None, extracts all environment variables.

None

Returns:

Name Type Description
DictDot DictDot

Object containing environment variables accessible via dot notation

Raises:

Type Description
ImportError

If python-dotenv package is not available

CredentialsError

If .env file doesn't exist or cannot be read

Example

Read all variables from .env file

creds = read_creds_from_dotenv() print(creds.DATABASE_URL) print(creds.API_KEY)

Read specific variables only

creds = read_creds_from_dotenv( ... env_path="config.env", ... params=["DOMO_INSTANCE", "DOMO_TOKEN"] ... ) print(creds.DOMO_INSTANCE) print(creds.DOMO_TOKEN)

Handle missing variables gracefully

print(creds.MISSING_VAR) # Returns None instead of error

Note
  • Requires python-dotenv package for .env file parsing
  • Missing environment variables return None when accessed
  • Environment variables are loaded into os.environ during execution
  • Supports standard .env file format with KEY=value pairs
Source code in src/crew_dcs/utils/read_creds_from_dotenv.py
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
def read_creds_from_dotenv(
    env_path: str = ".env",
    params: list[str] | None = None,
) -> utils_dd.DictDot:
    """
    Read credentials from .env file and return as DictDot object for easy access.

    Loads environment variables from the specified .env file and returns them
    as a DictDot object for convenient dot notation access.

    Args:
        env_path (str): Path to the .env file (default: ".env")
        params (list[str], optional): list of specific parameters to extract.
                                    If None, extracts all environment variables.

    Returns:
        DictDot: Object containing environment variables accessible via dot notation

    Raises:
        ImportError: If python-dotenv package is not available
        CredentialsError: If .env file doesn't exist or cannot be read

    Example:
        >>> # Read all variables from .env file
        >>> creds = read_creds_from_dotenv()
        >>> print(creds.DATABASE_URL)
        >>> print(creds.API_KEY)

        >>> # Read specific variables only
        >>> creds = read_creds_from_dotenv(
        ...     env_path="config.env",
        ...     params=["DOMO_INSTANCE", "DOMO_TOKEN"]
        ... )
        >>> print(creds.DOMO_INSTANCE)
        >>> print(creds.DOMO_TOKEN)

        >>> # Handle missing variables gracefully
        >>> print(creds.MISSING_VAR)  # Returns None instead of error

    Note:
        - Requires python-dotenv package for .env file parsing
        - Missing environment variables return None when accessed
        - Environment variables are loaded into os.environ during execution
        - Supports standard .env file format with KEY=value pairs
    """

    # Check if file exists
    if not os.path.exists(env_path):
        raise CredentialsError(env_path, f"Environment file not found at: {env_path}")

    try:
        # Load environment variables from file
        load_dotenv(env_path)

        # Determine which parameters to extract
        if params is None:
            # Get all environment variables
            params = list(os.environ.keys())

        # Extract specified parameters
        params_res = {}
        for param in params:
            param_str = str(param)
            value = os.environ.get(param_str)
            params_res[param_str] = value

        return utils_dd.DictDot(params_res)

    except (OSError, ValueError) as e:
        raise CredentialsError(  # noqa: B904
            env_path, f"Failed to read or parse environment file: {e!s}"
        )