0PricingLogin
AI Agents · Lesson

.env Files and python-dotenv

Loading .env files, .gitignore rules, and dotenv best practices.

The Problem with Shell Exports

Setting environment variables with export in the shell works, but it requires re-setting them in every new terminal session. Managing many variables this way is error-prone and not shareable with teammates.

.env files solve this by storing all project variables in one file that is loaded automatically.

The .env File Format

A .env file contains KEY=VALUE pairs, one per line. Lines starting with # are comments. Values can optionally be quoted. This simple format is understood by dozens of tools and frameworks.

# .env file (NEVER commit this file to git)

# Required API keys
OPENAI_API_KEY=sk-proj-your-real-key-here
SEARCH_API_KEY=tvly-your-tavily-key-here

# Optional settings with defaults
AGENT_MODEL=gpt-4o-mini
AGENT_MAX_STEPS=20
LOG_LEVEL=DEBUG

# Database (optional — disables memory storage if not set)
# DATABASE_URL=postgresql://user:pass@localhost/agentdb

# Environment identifier
ENV=development

All lessons in this course

  1. Environment Variables for Agents
  2. .env Files and python-dotenv
  3. Secrets Rotation and Security
  4. Configuration Profiles for Dev and Prod
← Back to AI Agents