env

env

Functions

Name Description
find_dotenvs Find all dotenv files named filename in the start directory and every
getdir Returns the value of environment variable envvar, assumed to represent a
getvar Returns the value of environment variable envvar. If this variable is not defined, returns default.
load_dotenvs Load all dotenv files found by :func:find_dotenvs, from the farthest

find_dotenvs

env.find_dotenvs(filename='.env', start_path=None)

Find all dotenv files named filename in the start directory and every parent directory up to the filesystem root.

Unlike :func:dotenv.find_dotenv, which stops at the first match, this function collects all matches along the directory tree, using :func:dotenv.find_dotenv to resolve the starting directory.

Args: filename: dotenv filename to look for (default: '.env'). usecwd: if True (default), start from the current working directory. Ignored when start_path is provided. Note: unlike :func:dotenv.find_dotenv, usecwd=False here is equivalent to usecwd=True because find_dotenv would otherwise use this module’s frame, not the caller’s. start_path: explicit starting directory, takes priority over usecwd.

Returns: List of :class:~pathlib.Path objects for every dotenv file found, ordered from closest (start directory) to farthest (root).

getdir

env.getdir(envvar, default=None, create=None)

Returns the value of environment variable envvar, assumed to represent a directory path. If this variable is not defined, returns default.

The environment variable can be defined in the users .bashrc, or in a file .env in the current working directory.

Args: envvar: the input environment variable default: the default path, if the environment variable is not defined default values are predefined for the following variables: - DIR_DATA : “data” (in current working directory) - DIR_STATIC : DIR_DATA/“static” - DIR_SAMPLES : DIR_DATA/“sample_products” - DIR_ANCILLARY : DIR_DATA/“ancillary” create: whether to silently create the directory if it does not exist. If not provided this parameter defaults to False except for DIR_STATIC, DIR_SAMPLES and DIR_ANCILLARY.

Returns: the path to the directory.

getvar

env.getvar(envvar, default=None)

Returns the value of environment variable envvar. If this variable is not defined, returns default.

The environment variable can be defined in the users .bashrc, or in a file .env in the current working directory.

Args: envvar: the input environment variable default: the default return, if the environment variable is not defined

Returns: the requested environment variable or the default if the var is not defined and a default has been provided.

load_dotenvs

env.load_dotenvs(filename='.env', start_path=None, override=True)

Load all dotenv files found by :func:find_dotenvs, from the farthest ancestor to the closest directory so that inner .env files take precedence over outer ones.

load_dotenv is called once per file found; variables already present in the environment are only overwritten when override=True (the default here, unlike the dotenv library default, to honour the closest-wins precedence).

Args: filename: dotenv filename to look for (default: '.env'). usecwd: if True, start from the current working directory. start_path: explicit starting directory. override: whether a closer file’s value overrides a value already loaded from a farther file (default: True).

Returns: The list of dotenv files that were loaded, closest first.