Environments, Dependencies & API Keys
Most "it works on my machine" bugs come from three things: the wrong Python environment, unpinned package versions, and mishandled API keys. Get these right once and save yourself hours.
Virtual environments
A virtual environment is an isolated folder with its own Python interpreter and packages, so project A's openai==1.x doesn't fight project B's.
python3 -m venv .venv
source .venv/bin/activate # Windows: .venv\Scripts\activate
python -m pip install --upgrade pip
pip install openai anthropic python-dotenv pydantic numpyModern alternative: uv, a very fast drop-in for pip/venv workflows:
uv venv && source .venv/bin/activate
uv pip install openai pydanticConda is common in data science, especially when you need non-Python binaries such as CUDA libraries.
Pinning dependencies
AI libraries change fast, and breaking API changes are common. Pin versions so builds are reproducible:
pip freeze > requirements.txt # exact versions of everything installed
pip install -r requirements.txt # recreate elsewhereBetter: declare direct dependencies in pyproject.toml and use a lock file (uv, Poetry, pip-tools). Rule of thumb: pin exact versions for apps, use ranges for libraries.
Secrets: API keys
An API key is a password that spends money. Leaked keys are scraped from public GitHub repos within minutes.
# .env (NEVER commit this file)
OPENAI_API_KEY=sk-...
ANTHROPIC_API_KEY=sk-ant-...# .gitignore
.env
.venv/
__pycache__/import os
from dotenv import load_dotenv
load_dotenv() # reads .env into environment variables
api_key = os.environ["OPENAI_API_KEY"] # fails loudly if missing (good)Official SDKs read standard variables (OPENAI_API_KEY, ANTHROPIC_API_KEY) automatically, so you rarely pass the key in code.
Revoke it immediately in the provider dashboard and create a new one. Deleting the commit isn't enough: git history and forks keep it. Also set spending limits and usage alerts on every provider account.
Secrets in other places
- Browsers and mobile apps: never embed keys. Call your own backend, which holds the key.
- CI: use the CI's encrypted secrets feature, never plain YAML.
- Logs: redact
Authorizationheaders and keys; don't log full request objects blindly. - Notebooks: don't paste keys into cells you'll share. Use
getpassor environment variables.
A clean project layout
my-ai-app/
├── .env # secrets (git-ignored)
├── .env.example # variable names with fake values (committed)
├── .gitignore
├── pyproject.toml # dependencies
├── src/app/
│ ├── config.py # reads env vars, one place
│ ├── llm.py # model calls, retries
│ └── prompts/ # prompt templates as files, versioned
├── evals/ # golden datasets and eval scripts
└── tests/# src/app/config.py: centralize configuration
from pydantic_settings import BaseSettings, SettingsConfigDict # pip install pydantic-settings
class Settings(BaseSettings):
model_config = SettingsConfigDict(env_file=".env")
openai_api_key: str
model: str = "gpt-4o-mini"
request_timeout_s: float = 30.0
settings = Settings()