.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=developmentAll lessons in this course
- Environment Variables for Agents
- .env Files and python-dotenv
- Secrets Rotation and Security
- Configuration Profiles for Dev and Prod