Dotenv API Reference¶
Auto-generated API documentation for the dotenv module.
dotenv
¶
.env file parser and loader — zero dependencies, stdlib only, Python 3.10+.
Part of zerodep: https://github.com/Oaklight/zerodep Copyright (c) 2026 Peng Ding. MIT License.
Drop-in replacement for python-dotenv core functionality.
Example::
load_dotenv() # load .env into os.environ
values = dotenv_values(".env") # parse without modifying environ
path = find_dotenv() # search up for .env file
find_dotenv(filename='.env', raise_error_if_not_found=False, usecwd=False)
¶
Walk up from the calling file's directory (or cwd) to find filename.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
filename
|
str
|
Name of the file to search for. |
'.env'
|
raise_error_if_not_found
|
bool
|
Raise IOError if the file is not found. |
False
|
usecwd
|
bool
|
Start from the current working directory instead of the calling file's directory. |
False
|
Returns:
| Type | Description |
|---|---|
str
|
Absolute path to the found file, or empty string if not found. |
Source code in dotenv/dotenv.py
get_key(dotenv_path, key_to_get, encoding='utf-8')
¶
Get a single value from a .env file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotenv_path
|
str | PathLike[str]
|
Path to the .env file. |
required |
key_to_get
|
str
|
The key to retrieve. |
required |
encoding
|
str
|
File encoding. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
str | None
|
The value for the key, or None if not found. |
Source code in dotenv/dotenv.py
set_key(dotenv_path, key_to_set, value_to_set, quote_mode='always', export=False, encoding='utf-8')
¶
Set a key=value pair in a .env file, creating it if needed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotenv_path
|
str | PathLike[str]
|
Path to the .env file. |
required |
key_to_set
|
str
|
The key to set. |
required |
value_to_set
|
str
|
The value to set. |
required |
quote_mode
|
str
|
Quoting strategy: "always", "auto", or "never". |
'always'
|
export
|
bool
|
Whether to prefix with |
False
|
encoding
|
str
|
File encoding. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
tuple[bool, str, str]
|
Tuple of (success, key, value). |
Source code in dotenv/dotenv.py
unset_key(dotenv_path, key_to_unset, quote_mode='always', encoding='utf-8')
¶
Remove a key from a .env file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotenv_path
|
str | PathLike[str]
|
Path to the .env file. |
required |
key_to_unset
|
str
|
The key to remove. |
required |
quote_mode
|
str
|
Unused, kept for API compatibility. |
'always'
|
encoding
|
str
|
File encoding. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
tuple[bool, str]
|
Tuple of (success, key). |
Source code in dotenv/dotenv.py
dotenv_values(dotenv_path=None, stream=None, verbose=False, interpolate=True, override=False, encoding='utf-8')
¶
Parse a .env file and return a dict without modifying os.environ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotenv_path
|
str | PathLike[str] | None
|
Path to the .env file. If None, uses |
None
|
stream
|
IO[str] | None
|
A text stream to read from (overrides dotenv_path). |
None
|
verbose
|
bool
|
Print a warning when the file is missing. |
False
|
interpolate
|
bool
|
Expand |
True
|
override
|
bool
|
Unused for dotenv_values (kept for API compatibility). |
False
|
encoding
|
str
|
File encoding. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
dict[str, str | None]
|
Dictionary mapping variable names to their values. |
Source code in dotenv/dotenv.py
load_dotenv(dotenv_path=None, stream=None, verbose=False, interpolate=True, override=False, encoding='utf-8')
¶
Read a .env file and set os.environ.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
dotenv_path
|
str | PathLike[str] | None
|
Path to the .env file. If None, uses |
None
|
stream
|
IO[str] | None
|
A text stream to read from (overrides dotenv_path). |
None
|
verbose
|
bool
|
Print a warning when the file is missing. |
False
|
interpolate
|
bool
|
Expand |
True
|
override
|
bool
|
If True, overwrite existing environment variables. |
False
|
encoding
|
str
|
File encoding. |
'utf-8'
|
Returns:
| Type | Description |
|---|---|
bool
|
True if a file was found and loaded. |