load-dotenv

0.1.0 · active · verified Thu Apr 16

The `load-dotenv` library, currently at version 0.1.0, provides a lightweight wrapper that automatically and implicitly loads environment variables from `.env` files. It essentially re-exports the core functionality of the popular `python-dotenv` library, offering a simple interface to manage environment variables for development. It has a low release cadence given its wrapper nature.

Common errors

Warnings

Install

Imports

Quickstart

Initializes environment variables by calling `load_dotenv()`. This will search for a `.env` file in the current directory and its parents, loading any key-value pairs found into `os.environ`. Access variables using `os.getenv()`.

import os
from load_dotenv import load_dotenv

# Create a dummy .env file for demonstration
# In a real scenario, this file would exist beforehand
with open('.env', 'w') as f:
    f.write('DB_HOST=localhost\n')
    f.write('DB_USER=admin\n')
    f.write('DB_PASSWORD=secret_password\n')

load_dotenv() # take environment variables from .env.

# Access environment variables
db_host = os.getenv("DB_HOST")
db_user = os.getenv("DB_USER")
db_password = os.getenv("DB_PASSWORD")

print(f"DB Host: {db_host}")
print(f"DB User: {db_user}")
print(f"DB Password: {db_password}")

# Clean up the dummy .env file
os.remove('.env')

view raw JSON →