Skip to main content

A simple module to load environment variables from a .env file

Project description

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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

dotvar-0.1.12.tar.gz (32.9 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

dotvar-0.1.12-py3-none-any.whl (4.0 kB view details)

Uploaded Python 3

File details

Details for the file dotvar-0.1.12.tar.gz.

File metadata

  • Download URL: dotvar-0.1.12.tar.gz
  • Upload date:
  • Size: 32.9 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for dotvar-0.1.12.tar.gz
Algorithm Hash digest
SHA256 033bea448067b29be230b0fbf3c97fa5ff4a875471ddb1cb505049698c50e774
MD5 855ef828138d62137cc69d9d4206ec6a
BLAKE2b-256 76d8d8ef6b289aec21bfaae97ca86068403bf541e5d1c5c7c14897e06d5d1beb

See more details on using hashes here.

File details

Details for the file dotvar-0.1.12-py3-none-any.whl.

File metadata

  • Download URL: dotvar-0.1.12-py3-none-any.whl
  • Upload date:
  • Size: 4.0 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: uv/0.9.3

File hashes

Hashes for dotvar-0.1.12-py3-none-any.whl
Algorithm Hash digest
SHA256 9e11ad6cd0a6341c824ff70bd4ca7e9bb2f3e64df73a51e86b87d61bfb06272a
MD5 20cac51fae0c02e6473d5c3c78b41450
BLAKE2b-256 26b0ae474bae166b2f00f8288cd47ac9acef3e0d30bdab5457776caa1746a8eb

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page