dotvar
is a Python module that improves upon traditional .env
loaders by providing automatic loading and native variable interpolation, solving the major shortcomings of python-dotenv
.
Differences from python-dotenv
:
- ✅ Built-in Interpolation: Reference other variables within
.env
. - ✅ Option to Auto-load: The option to load upon import, with no extra calls.
- ✅ Strict Mode: Catch missing
.env
files early. - ✅ No Dependencies: Pure Python, standard library only.
- ✅ Lightweight & Fast: Minimal footprint, optimized load time.
- place a
.env
in your current folder - you can run
load_env
or simply importdotvar.auto_load
which has the side-effect of callingload_env
Alternatively, there is also aimport dotvar.auto_load # noqa
strict
mode, that raises an error if an environment file is not found.import dotvar.auto_load_strict # noqa
Alternative syntax
If the import side-effect is undesirable, you can import the load_env
function and call it imperatively.
The import here will NOT have a side-effect.
from dotvar import load_env
load_env(strict=False)
The strict flag defautls to False.
While python-dotenv
is widely used, it has two significant limitations:
- It does not support automatic loading upon import.
- It lacks variable interpolation, so environment variables cannot reference other variables within the same
.env
file.
For example, the following will not be correctly resolved using python-dotenv
:
BASE_URL=https://api.example.com
API_ENDPOINT=${BASE_URL}/v1/
import os
print(os.environ.get("API_ENDPOINT")) # Returns "${BASE_URL}/v1/" instead of the resolved value
dotvar
solves these issues by:
- Supporting native variable interpolation with
${VAR_NAME}
syntax. - Offering an auto-load entrypoint: just import
dotvar.auto_load
. - Providing a strict mode (
dotvar.auto_load_strict
) that raises an error if.env
is not found.
pip install dotvar # supports Python 3.7+
An Example:
Place a .env
file in your project root:
BASE_URL=https://api.example.com
API_ENDPOINT=${BASE_URL}/v1/
API_KEY=s3cr3t_api
Then in your Python code:
# noinspection PyUnresolvedReferences
import dotvar.auto_load # noqa
import os
print(os.environ["BASE_URL"]) # https://api.example.com
print(os.environ["API_ENDPOINT"]) # https://api.example.com/v1/
print(os.environ["API_KEY"]) # s3cr3t_api
To use strict mode:
import dotvar.auto_load_strict # noqa
This project uses Poetry for dependency management.
# Set up environment
poetry install
# Run all tests
poetry run pytest
# Run with coverage
poetry run pytest --cov=dotvar --cov-report=term-missing
MIT License