Loading¶
Environment variable and .env file loading logic.
loading ¶
Environment variable and .env file loading logic.
load_env_files ¶
load_env_files(
env: str | None = None,
*,
override: bool = True,
env_dir: Path | None = None,
) -> dict[str, str]
Load environment variables from cascading .env files.
This function implements Node.js-style .env file cascading, loading files
in the following order (later files override earlier):
1. .env (base configuration)
2. .env.local (local base overrides)
3. .env.{env} (environment-specific)
4. .env.{env}.local (local environment overrides)
When to use
- Called automatically by
DotEnvConfig.load()— you rarely call this directly - Call directly if you need to load .env files without creating a config
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
env
|
str | None
|
Environment name (e.g., "dev", "prod", "test"). If None, reads from
the |
None
|
override
|
bool
|
If True, .env file values override existing environment variables. If False, existing env vars take precedence |
True
|
env_dir
|
Path | None
|
Custom base directory for .env files. If None, uses
the |
None
|
Returns:
| Type | Description |
|---|---|
dict[str, str]
|
Dictionary of all environment variables after loading |
Raises:
| Type | Description |
|---|---|
ValueError
|
If |
FileNotFoundError
|
If |
Example
See Also
DotEnvConfig.load: Loads config and .env files.get_env_var: Get a single env var by field name.
Source code in dotenvmodel/loading.py
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 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 112 113 114 115 116 117 118 119 | |
get_env_var ¶
Get environment variable value by field name or alias.
When to use
- Called internally by
DotEnvConfig.load()— rarely called directly - Use directly if you need to check a config env var without loading the full config
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_name
|
str
|
Name of the field (converted to UPPER_CASE for env var lookup) |
required |
alias
|
str | None
|
Optional alias that overrides the field name for env var lookup.
When provided, |
None
|
prefix
|
str | None
|
Optional class-level prefix to prepend to the env var name.
Not applied when |
None
|
Returns:
| Type | Description |
|---|---|
str | None
|
Environment variable value as string, or None if not set |
See Also
get_env_var_name: Get just the name, not the value.
Source code in dotenvmodel/loading.py
get_env_var_name ¶
Get the environment variable name for a field.
When to use
- For generating documentation or .env.example files
- For error messages that reference the env var name
- Called internally during config loading
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
field_name
|
str
|
Name of the field (converted to UPPER_CASE for env var lookup) |
required |
alias
|
str | None
|
Optional alias that overrides the field name. When provided,
|
None
|
prefix
|
str | None
|
Optional class-level prefix to prepend. Not applied when
|
None
|
Returns:
| Type | Description |
|---|---|
str
|
The environment variable name string |
Example
See Also
get_env_var: Get the value, not just the name.