Environment Variables & .env Files Explained is an essential skill for modern operators. This guide covers everything you need to get started.
What You Need to Know
Environment variables are the secure, portable way to store configuration and secrets in your AI applications — separating sensitive credentials from your code.
Instead of hardcoding API keys, database URLs, or configuration values directly in your scripts, environment variables let you inject them at runtime — different values for development, staging, and production environments.
Operators who manage environment variables correctly avoid accidental credential leaks to GitHub, enable easy deployment across environments, and follow the security best practices all professional AI projects require.
Getting Started: Step by Step
- Understand what env vars are — Environment variables are key-value pairs stored in your operating system's environment, accessible to any process.
- Set env vars on your system — Use 'export KEY=VALUE' in bash/zsh or the System Properties dialog on Windows to set persistent environment variables.
- Use .env files for local development — Create a .env file in your project root and load it with python-dotenv to manage local development secrets.
- Access env vars in Python — Use 'os.environ.get("KEY")' or python-dotenv's load_dotenv() to safely read environment variables in your scripts.
- Never commit .env files to git — Add '.env' to your .gitignore immediately — never let secrets enter version control.
Key Tools
- python-dotenv — Python library that loads environment variables from .env files into os.environ automatically.
- direnv — Shell extension that automatically loads/unloads .env files when you enter/leave project directories.
- Doppler — Team secrets management platform syncing environment variables across all environments and team members.
The operators who move fast on this don't wait for perfect conditions. They start, iterate, and improve. Come build with us at skool.com/aiguerrilla.
Ready to Go Deeper?
Join 150+ operators applying AI in the real world. Free community, real results.
Join AI Guerrilla Free →Next Steps
The best way to go deeper is to join fellow operators at skool.com/aiguerrilla — a free community where hundreds of practitioners share what's actually working.