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
.envfiles early. - ✅ No Dependencies: Pure Python, standard library only.
- ✅ Lightweight & Fast: Minimal footprint, optimized load time.
If you use Claude Code, add the dotvar skill to your CLAUDE.md:
# add https://raw.githubusercontent.com/geyang/dotvar/main/skill/dotvar.md as a skillor
@import https://raw.githubusercontent.com/geyang/dotvar/main/skill/dotvar.mdThis teaches Claude how to use dotvar in your projects.
- place a
.envin your current folder - you can run
load_envor simply importdotvar.auto_loadwhich has the side-effect of callingload_envAlternatively, there is also aimport dotvar.auto_load # noqa
strictmode, 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
.envfile.
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 valuedotvar 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.envis not found.
pip install dotvar # supports Python 3.9+An Example:
Place a .env file in your project root:
BASE_URL=https://api.example.com
API_ENDPOINT=${BASE_URL}/v1/
API_KEY=s3cr3t_apiThen 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_apiTo use strict mode:
import dotvar.auto_load_strict # noqaThis project uses uv for dependency management.
# Set up environment
uv sync
# Run all tests
uv run pytest
# Run with coverage
uv run pytest --cov=dotvar --cov-report=term-missingMIT License