πŸ› οΈToolsShed

.env File Parser

Parse, validate, and format .env files.

Frequently Asked Questions

Code Implementation

# pip install python-dotenv
from dotenv import load_dotenv
import os

# Load .env file into environment variables
load_dotenv()  # Looks for .env in the current directory

# Access variables
db_url = os.getenv("DATABASE_URL")
api_key = os.getenv("API_KEY", "default-value")  # With fallback

print(f"DB URL: {db_url}")
print(f"API Key: {api_key}")

# Load a specific file
load_dotenv(".env.production")

# Load without overriding existing env vars
load_dotenv(override=False)

# Parse .env content directly (without touching os.environ)
from dotenv import dotenv_values

config = dotenv_values(".env")
print(config)
# OrderedDict([('DATABASE_URL', 'postgres://...'), ('API_KEY', 'sk-...')])

# Example .env file:
# DATABASE_URL=postgres://user:pass@localhost:5432/mydb
# API_KEY=sk-abc123
# DEBUG=true
# # This is a comment

Comments & Feedback

Comments are powered by Giscus. Sign in with GitHub to leave a comment.