Almost every AI agent tutorial opens with a few setup lines — create an environment, install some packages, paste in an API key — and assumes you already know the ritual. Land on Part 4 of a series from a search result and those lines are three parts behind you. This environment setup primer is the one place that spells the whole thing out, so any tutorial on the site is ready to run in about five minutes.
There are only four moving parts: Python itself, an isolated environment for the project, the packages the tutorial needs, and your API key kept safely out of the code. Do them once in order and they become a habit you stop thinking about. Here's the full flow, with links to the deeper primers when you want the why behind each step.
- A computer where you can install software and open a terminal (Terminal on mac/Linux, PowerShell on Windows)
- No prior Python packaging knowledge — every step below is explained from zero
- Four steps get any tutorial running: install Python, create a virtual environment, install the packages, add your API key.
- Do the environment and key per project, not once globally — that's what keeps two tutorials from breaking each other.
- Keep secrets in a
.envfile, never in your code, and never commit it. - venv + pip or uv — both work; use whichever the tutorial shows.
Step 1 — Install Python#
Agent tutorials on this site target Python 3.11 or newer. Check what you have first:
python3 --version # e.g. Python 3.12.3If that prints 3.11+ you're set. If the command is missing or the version is older, install the latest from python.org/downloads (on macOS, brew install python also works). You install Python once — everything after this is per project.
Step 2 — Create an isolated environment#
Never install packages straight into your system Python. Give each project its own virtual environment so one tutorial's dependencies can't clash with another's:
python3 -m venv .venv # create an env in a .venv folder
source .venv/bin/activate # activate it (mac/Linux)
# .venv\Scripts\activate # (Windows PowerShell)Your prompt now shows (.venv) — the signal that the box is open. Everything you install from here lands inside this folder and nowhere else. Prefer the faster modern tool? uv venv does the same thing in seconds. Either is fine.
Activate the environment before you install or run anything. A package installed with no env active goes to the global Python, and your "ModuleNotFoundError" is really a "wrong environment" error in disguise.
New to any of this? The venv, pip & uv primer covers virtual environments end to end.
Step 3 — Install the tutorial's packages#
With the environment active, install whatever the tutorial lists. Upgrade pip first so you're not fighting old-tooling warnings:
pip install -U pip
pip install openai # or anthropic, langgraph, fastapi, … per the guideEach tutorial names its own packages near the top. Install them into this environment, and if you ever want to reproduce the setup later, run pip freeze > requirements.txt and commit that file — not the .venv folder.
Step 4 — Add your API key with a .env file#
Agents call a model provider (OpenAI, Anthropic, Google), and that needs an API key. Keep the key out of your code — put it in a .env file in the project root:
# .env
OPENAI_API_KEY=sk-...your-key-here...Load it in Python with python-dotenv (pip install python-dotenv), and the provider SDK picks the key up automatically:
from dotenv import load_dotenv
load_dotenv() # reads .env into the environmentWhere to get the key, free tiers, and spending limits are covered in the LLM API key + .env primer — read it once and you're covered for every provider.
Before your first commit, add both of these to .gitignore: \.env and .venv/. The first keeps your secret key out of the repo; the second keeps thousands of machine-specific files out. A leaked key in git history is the single most common — and most expensive — beginner mistake.
Step 5 — Verify it runs#
One command confirms the environment, packages, and key are all wired up before you start the real tutorial:
python -c "from openai import OpenAI; OpenAI(); print('setup OK')"If it prints setup OK, you're ready — jump back into whatever guide sent you here. If it errors, it's almost always one of three things: the environment isn't activated, the package isn't installed in it, or the key isn't in .env. Re-check Steps 2–4 in that order.
Quick recap#
The whole setup, in five lines:
- Install Python 3.11+ once — check with
python3 --version. - Create a virtual environment per project:
python3 -m venv .venv→ activate. - Install the tutorial's packages into that active environment.
- Add your API key to
.envand load it withpython-dotenv. - Gitignore
.envand.venv/, then verify with a one-line import.
Frequently Asked Questions#
What do I need before starting a tutorial? Python 3.11+, a virtual environment, the tutorial's packages, and an API key in a .env file.
Do I repeat this every time? Install Python once; do the environment, packages, and key per project so tutorials stay isolated.
Where do I get an API key? From your provider's dashboard (OpenAI/Anthropic/Google) — most are pay-per-use with a small free tier. See the API key primer.
venv or uv? Either — they're interchangeable for learning. Use whichever the tutorial shows.
Conclusion#
Environment setup is the least glamorous part of building agents and the one that trips up the most beginners — but it's just four steps you'll soon run without thinking. Install Python, isolate the project, install the packages, keep your key in .env, and verify. Five minutes of housekeeping, and every tutorial on the site runs clean the first time.
Bookmark this page — it's the setup any guide here assumes. Then head back to the tutorial you came from.
- Want the environment detail? The venv, pip & uv primer explains isolation properly.
- Setting up your key? The LLM API key + .env primer covers providers, free tiers, and safety.
- Ready to build? Start with Call an LLM in Python in your fresh environment.