Skip to main content

dotvar: Smarter Environment Variable Management for Python

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.

How to Use

  1. place a .env in your current folder
  2. you can run load_env or simply import dotvar.auto_load which has the side-effect of calling load_env
    import dotvar.auto_load  # noqa
    
    Alternatively, there is also a 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.

The Problems with python-dotenv

While python-dotenv is widely used, it has two significant limitations:

  1. It does not support automatic loading upon import.
  2. 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

Introducing dotvar

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.

Installation and Examples

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

Development

This 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-missing

License

MIT License

Metadata

Release files for dotvar 0.1.12

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dotvar 0.1.12
File Size Uploaded
dotvar-0.1.12.tar.gz 32.9 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dotvar 0.1.12
File Interpreter ABI Platform
dotvar-0.1.12-py3-none-any.whl Python 3 none any Details

Total release size: 36.9 kB

Release files / dotvar-0.1.12.tar.gz

Download URL dotvar-0.1.12.tar.gz
Size 32.9 kB
Tags Source
SHA-256 checksum
How to use checksums
033bea448067b29be230b0fbf3c97fa5ff4a875471ddb1cb505049698c50e774
BLAKE2b-256 checksum
How to use checksums
76d8d8ef6b289aec21bfaae97ca86068403bf541e5d1c5c7c14897e06d5d1beb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.3

Release files / dotvar-0.1.12-py3-none-any.whl

Download URL dotvar-0.1.12-py3-none-any.whl
Size 4.0 kB
Tags Python 3
SHA-256 checksum
How to use checksums
9e11ad6cd0a6341c824ff70bd4ca7e9bb2f3e64df73a51e86b87d61bfb06272a
BLAKE2b-256 checksum
How to use checksums
26b0ae474bae166b2f00f8288cd47ac9acef3e0d30bdab5457776caa1746a8eb
Upload date
Uploaded using Trusted Publishing?
What is trusted publishing?
No
Uploaded via uv/0.9.3

Release history Release notifications | RSS feed

This release

0.1.12 This release

2 release files

0.1.1

2 release files

0.1.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page