2.0 What This Teaches
- Python 3.10+ and creating a virtual environment
- Installing
anthropicand optional packages - Getting an API key from console.anthropic.com
- Storing the key safely in an environment variable
- Using a
.envfile withpython-dotenv - Verifying the setup with a one-line test
2.1 Python 3.10 and Virtual Environments
# create and activate a virtual environment
python -m venv .venv
# Windows PowerShell
.venv\Scripts\Activate.ps1
# macOS / Linux
source .venv/bin/activate
(.venv) when the environment is active.
All pip install commands from this point install into .venv
only, not system Python.
2.2 Installing the SDK
pip install anthropic
pip install anthropic flask
.env file (covered in section 2.5):
pip install python-dotenv
pip freeze > requirements.txt
pip install -r requirements.txt
2.3 Getting an API Key
sk-ant-. Treat it like a password: never put it in
source code, never commit it to version control, and revoke it if you believe it was
exposed.
2.4 Setting the Environment Variable
ANTHROPIC_API_KEY from the environment automatically.
Set it for the current terminal session:
# macOS / Linux
export ANTHROPIC_API_KEY="sk-ant-your-key-here"
# Windows PowerShell
$env:ANTHROPIC_API_KEY = "sk-ant-your-key-here"
~/.bashrc or ~/.zshrc. On Windows, use
System Properties → Environment Variables.
2.5 Using a .env File
.env file in the project root stores the key so you don't have to
set the environment variable every time you open a terminal.
# .env (never commit this file)
ANTHROPIC_API_KEY=sk-ant-your-key-here
.env to .gitignore immediately:# .gitignore
.env
.venv/
__pycache__/
python-dotenv:# Load .env before creating the client.
from dotenv import load_dotenv
load_dotenv()
import anthropic
client = anthropic.Anthropic() # ANTHROPIC_API_KEY is now in the environment
2.6 Verifying the Setup
# verify.py - confirms SDK and API key are working.
import anthropic
client = anthropic.Anthropic()
response = client.messages.create(
model="claude-haiku-4-5-20251001",
max_tokens=32,
messages=[{"role": "user", "content": "Say OK."}]
)
print(response.content[0].text)
OK
claude-haiku-4-5-20251001 here keeps the verification call cheap
and fast. Any response means the key, network, and SDK are all working.
2.7 Exercise
Exercise
Create a fresh project folder with a virtual environment. Install
anthropic and python-dotenv. Create a .env
file with your API key, add it to .gitignore, and run
verify.py. Then run pip freeze > requirements.txt
and inspect the file to see which packages were installed.
2.8 Common Mistakes
Installing without a virtual environment
.venv before running any
pip install in a project.
Committing the .env file or the key in source code
Forgetting to activate the virtual environment
python verify.py without activating .venv uses
system Python, which does not have the anthropic package. The error
is ModuleNotFoundError: No module named 'anthropic'.
2.9 Key Terms
| Term | Meaning |
|---|---|
| virtual environment | An isolated Python installation for one project; packages installed here don't affect others |
| pip | Python's package installer; downloads packages from PyPI |
| requirements.txt | A file listing exact package versions; used to recreate an environment |
| API key | A secret token that authenticates your requests to Anthropic's servers |
| ANTHROPIC_API_KEY | The environment variable the SDK reads for the API key |
| .env file | A plain-text file storing environment variables; loaded by python-dotenv |
| python-dotenv | A package that loads .env into the process environment at startup |