Using VS Code for This Course
One place to learn the exact setup you'll use for every chapter, lab, and project: how to open the course, run Python and the build labs, manage virtual environments, keep API keys safe, and use the Claude / Anthropic VS Code extension as your AI pair-programmer. Do this once and every later step just works.
python / pytest. This page sets that up once. If you've never used VS Code, follow it top to bottom; if you have, skim the environment and extension sections — those trip people up most.By the end of this page you will
- Have VS Code, the Python extension, and the Anthropic extension installed and working.
- Know how to open a project folder and use the integrated terminal.
- Create and activate a per-project virtual environment — and know why the terminal prompt changes.
- Install all course dependencies into that venv from
code/requirements.txtin one command. - Store your API key safely (never in code) and confirm it's picked up.
- Use the Anthropic extension to explain, generate, and debug code as you work through the course.
- Pin the interpreter with a
.vscode/settings.jsonso it's remembered, and (macOS/Linux) auto-activate the venv by folder. - Recognise and fix the classic traps: wrong interpreter, the
.code-workspaceoverride, and cloud-sync + venv problems.
1 · Install VS Code and the extensions
You need three things: the editor and two extensions. Install in this order.
- VS Code — download from
code.visualstudio.comand install for your OS. - Python extension (Microsoft) — open the Extensions view (Cmd/Ctrl+Shift+X), search "Python", install the one by Microsoft. This gives you interpreter selection, the run button, and test integration.
- Anthropic extension — in the same Extensions view, search "Claude" / "Anthropic" and install the official extension. This is your in-editor AI assistant (you've already configured it — the next sections show how to use it for the course).
2 · Open the course / a project folder
VS Code works on a folder (your "workspace"), not loose files. Always open the folder that contains the code you're working on.
- File → Open Folder… and pick your working directory (for a project lab, the project folder you create — e.g.
content-assistant/). - Or from a terminal already in that folder:
code .(the dot means "this folder"). - The Explorer panel on the left now lists your files. Click one to edit it.
python -m pytest tests/ that assume you're inside the project folder. If imports fail with ModuleNotFoundError, you almost certainly opened the wrong folder or your terminal is one level up. Open the exact project folder.3 · The integrated terminal
Everything the labs ask you to "run in your terminal" happens in VS Code's built-in terminal — no separate app needed.
- Open it with Terminal → New Terminal, or the shortcut Ctrl+` (the backtick key, top-left).
- It opens already inside your workspace folder — so
pwdshows the folder you opened. - Type the lab's commands here and press Enter. Output appears inline.
(env-name) at the start of your promptIf your prompt looks like (agents) you@machine project %, the text in parentheses is the active Python environment. It's not an error — it just tells you which environment your python/pip commands will use. You want it to say your project's .venv (next section), not a leftover global one. To leave an environment: run conda deactivate (conda) or deactivate (venv).4 · Virtual environments — the step people skip
A virtual environment (venv) is a private package folder for one project, so installing something for the Content Assistant doesn't collide with another project. Every build lab starts by creating one. Here's the whole lifecycle.
terminal — macOS / Linuxpython3 -m venv .venv # create it (once per project)
source .venv/bin/activate # activate it (each new terminal)
terminal — Windows (PowerShell)py -m venv .venv
.venv\Scripts\Activate.ps1
(.venv) you@machine content-assistant % # prompt now shows (.venv) — success
This is the whole life of a virtual environment (venv) — a private package folder for one project so its libraries never clash with another project's. Pick the block for your operating system; the commands do the same two things.
python3 -m venv .venv(Windows:py -m venv .venv) creates the environment in a hidden folder called.venv. You do this once per project — the folder then just sits there.source .venv/bin/activate(Windows:.venv\Scripts\Activate.ps1) turns it on for the current terminal. You repeat this every time you open a new terminal — activation does not stick.- After activating,
pythonandpippoint into.venv, so anything you install lands there and nowhere else.
What the output means: Your prompt gains a (.venv) prefix, e.g. (.venv) you@machine content-assistant %. That prefix is your proof the environment is active — no prefix means you're back on the system Python.
Try this: Run which python (Windows: where python) before and after activating. Before, it points at a system path; after, it points inside .venv. That is exactly what changed.
(.venv) only lasts for the terminal window it was activated in. Close the terminal (or open a new one) and you must run the activate line again — otherwise python/pip use the wrong environment and your installed packages "disappear". If a lab command suddenly can't find a package, check your prompt shows (.venv) first..venv. VS Code then auto-activates it in new terminals. If you saw (agents) auto-appear, it's because a different interpreter was selected here — switch it to your project's .venv.4b · Install the course dependencies
With the venv activated ((.venv) showing in your prompt), install the packages the labs use. The course ships a single requirements file — one command installs everything, so no lesson surprises you with a missing module.
terminal — inside your activated venvcd path/to/llm-course/code # where requirements.txt lives
pip install -r requirements.txt # installs all course + AWS libraries
Successfully installed anthropic boto3 sagemaker langgraph crewai ...
With the venv active ((.venv) showing), this installs every library the course uses in one shot, reading the list from a requirements file — so no lesson later fails on a missing module.
cd path/to/llm-course/codemoves you into the folder that containsrequirements.txt. Thecd("change directory") step matters because the next command looks for that file in the current folder.pip install -r requirements.txttellspipto read the file (-r= "from this requirements list") and download every library named in it into your active venv.
What the output means: A stream of downloads ending in Successfully installed anthropic boto3 …. Each name is one library now available to import in your code.
Try this: If you only need a couple of libraries for one chapter, you can skip the big file and run e.g. pip install anthropic on its own — same command, just one package.
That file lives at llm-course/code/requirements.txt and pins the third-party libraries used across the course. What you get, grouped by where it's used:
| Library | Used in |
|---|---|
anthropic | Claude & Anthropic chapters (the SDK) |
boto3 | All AWS AI Automation chapters (W1–W14) — the AWS SDK |
sagemaker | AWS W8–W9 (SageMaker deploy & pipelines) |
aws-cdk-lib, constructs | AWS W7 & the CDK projects (infrastructure as code) |
langchain-core, langchain-anthropic, langgraph, langsmith | LangChain & LangGraph chapters |
crewai, autogen-agentchat | Multi-agent orchestration chapters |
dspy | Prompt-optimization chapter |
mcp | MCP server project |
numpy, pandas, matplotlib, seaborn, scikit-learn | Data & app-building, NLP chapters |
pydantic, pytest | Structured output & testing, used throughout |
pip install boto3 is enough. Each lesson's ▶ Runnable companion folder has its own README naming just the packages that lesson needs.(.venv), pip install puts packages in the wrong place and VS Code won't find them — the classic "boto3 is not recognized" symptom. Activate first (section 4), confirm (.venv), then install.boto3 makes the import resolve so you can read and edit the AWS labs. Actually calling AWS (Bedrock, S3, …) additionally needs aws configure credentials and, for Bedrock, model access enabled in your region — see W1 · AWS AI foundations.5 · Running code and tests
Two ways to run — pick whichever you like; the labs show terminal commands because they're unambiguous.
| Task | Terminal (used in the labs) | VS Code UI |
|---|---|---|
| Run a script | python engine.py | Open the file → click the ▷ Run button (top-right) |
| Run the tests | python -m pytest tests/ -v | Open the Testing panel (flask icon) → Run All |
| Install a package | pip install pytest | — |
| Stop a running program | Ctrl+C in the terminal | — |
pytest suites are designed to pass with no API key (they use mocks/stubs). So you can run them immediately after pip install pytest — a great way to confirm your environment is healthy before wiring in a real key.6 · API keys, the safe way
When a lab's optional "go live" step needs the real Claude API, it reads your key from an environment variable — never from code you might commit.
terminal — macOS / Linuxexport ANTHROPIC_API_KEY="sk-ant-your-key-here"
terminal — Windows (PowerShell)$env:ANTHROPIC_API_KEY="sk-ant-your-key-here"
Confirm it's set (should print your key):
terminalecho $ANTHROPIC_API_KEY # Windows: echo $env:ANTHROPIC_API_KEY
An API key is your private password for the Claude API. You store it in an environment variable — a named value the shell holds in memory — so your code can read it without the key ever being written into a file you might share or commit.
export ANTHROPIC_API_KEY="sk-ant-…"(Windows PowerShell:$env:ANTHROPIC_API_KEY="sk-ant-…") puts your key into that named variable for the current terminal session.echo $ANTHROPIC_API_KEY(Windows:echo $env:ANTHROPIC_API_KEY) prints the value back so you can confirm it was set.echojust means "print this to the screen".- The Anthropic SDK looks for this exact variable name automatically, so your Python code never has to contain the key itself.
What the output means: The echo line should print your key (starting sk-ant-). If it prints a blank line, the variable isn't set — re-run the export line in this same terminal.
Try this: Open a brand-new terminal and run the echo again — it will be blank, because export only lasts for the terminal you ran it in. That's why the tip below shows how to make it stick.
sk-ant-… into a .py file. Use the environment variable above, or a .env file that is listed in .gitignore. The Anthropic SDK reads ANTHROPIC_API_KEY from the environment automatically, so you never reference the key value in code. If a key is ever committed, rotate it immediately in the Anthropic Console.export above lasts only for that terminal. To set it for every new terminal, add the line to your shell profile (~/.zshrc on modern macOS, ~/.bashrc on Linux) — but only on a machine you control, and never in a file inside a git repo.7 · Using the Anthropic extension for the course
You've configured the Claude / Anthropic extension — here's how to actually use it as you learn. It's an AI pair-programmer living in the editor: it can read your open files, explain code, generate it, and help debug.
| When you're… | Ask the extension to… |
|---|---|
| Reading a lab's code file | "Explain this file line by line" — great for the schema/validator and agent-loop code. |
| Stuck on a failing test | Paste the error and ask "why does this test fail and how do I fix it?" |
| Extending a project | "Add a new field to this Pydantic model and update the validator" — then run the tests. |
| Understanding an error | Select the traceback → ask "what does this mean?" It maps to the labs' troubleshooting tables. |
| Learning a concept | "Explain what a virtual environment is and why this lab uses one." |
- Open any lab's code file (say
engine.py). - Open the extension's chat panel (from the Activity Bar icon or the Command Palette).
- Ask: "Explain what this file does and how I'd run it, assuming I'm new to Python."
- Then try: "I ran the tests and got
ModuleNotFoundError— what's wrong?" and compare its answer to the lab's Troubleshooting table.
8 · Your per-lab workflow (memorize this)
Every project lab follows the same rhythm. Once this is muscle memory, the whole course is smooth:
1. Open the project folder in VS Code (File -> Open Folder)
2. Open a terminal (Ctrl + `)
3. Activate the venv source .venv/bin/activate
-> confirm the prompt shows (.venv)
4. Create files by pasting the lab's blocks (Explorer -> New File)
5. Run it python .py
6. Run the tests python -m pytest tests/ -v
7. (optional) set the key + go live export ANTHROPIC_API_KEY=...
8. Ask the Anthropic extension when stuck
This is not code to run — it's the rhythm every lab follows, listed in order. Read it as a checklist you repeat for each project until it's muscle memory.
- Steps 1–2 get you set up: open the project folder (not a loose file) and open the integrated terminal.
- Step 3 activates the venv — and the crucial confirmation is that your prompt now shows
(.venv). If it doesn't, stop and fix that before continuing. - Steps 4–6 are the actual work: create the lab's files, run the script with
python <file>.py, then run the tests withpython -m pytest tests/ -v. - Steps 7–8 are optional: set your API key to "go live", and ask the Anthropic extension whenever you're stuck.
Try this: The two lines that trip up beginners are steps 3 and 6. If a command "can't find" a package, you almost always skipped step 3 (activation).
9 · Make it automatic — pin the interpreter with a settings file
The steps above work, but you can make VS Code remember the interpreter and testing config for a folder so you don't re-select it each time. VS Code reads a per-folder file at .vscode/settings.json inside your project folder. Create it once.
In your project folder, create a folder named .vscode, and inside it a file settings.json. Replace the interpreter path with your venv's Python (run which python while the venv is active to get it):
<your-project>/.vscode/settings.json
.vscode/settings.json{
"python.defaultInterpreterPath": "/ABSOLUTE/PATH/TO/.venv/bin/python",
"python.terminal.activateEnvironment": true,
"python.testing.pytestEnabled": true,
"python.testing.pytestArgs": ["."],
"terminal.integrated.cwd": "${workspaceFolder}",
"python.analysis.extraPaths": [
"/ABSOLUTE/PATH/TO/.venv/lib/python3.13/site-packages"
]
}
This is a configuration file, not a program — VS Code reads it automatically for this folder. It's written in JSON: a set of "name": value pairs inside curly braces, separated by commas. It saves you re-selecting the interpreter every time.
"python.defaultInterpreterPath"points VS Code at your venv's Python. Replace the/ABSOLUTE/PATH/TO/.venv/bin/pythonplaceholder with the real path (runwhich pythonwhile the venv is active to get it)."python.terminal.activateEnvironment": truemakes new terminals turn the venv on for you."python.testing.pytestEnabled"plus"python.testing.pytestArgs": ["."]wire up the Testing panel to find and run yourpytestsuite in the current folder."python.analysis.extraPaths"tells the code checker where the installed packages live, which clears the red underline under yourimportlines.
What the output means: No visible output — VS Code silently uses these settings. You'll notice the effect: the correct interpreter is pre-selected and the red squiggles under imports disappear.
Try this: After saving, run Developer: Reload Window from the Command Palette so VS Code re-reads the file. JSON is picky — a missing comma or a stray trailing comma will make it ignore the whole file.
| Setting | What it does |
|---|---|
python.defaultInterpreterPath | Selects your venv automatically (no "Enter interpreter path" each time) |
python.terminal.activateEnvironment | Auto-activates the venv in new terminals |
python.testing.pytestEnabled + pytestArgs | Wires up the Testing panel to run your pytest suite |
python.analysis.extraPaths | Tells Pylance where the packages are, so imports resolve (kills the red underline) |
.vscode/launch.json:
.vscode/launch.json{
"version": "0.2.0",
"configurations": [{
"name": "Python: Current File (my venv)",
"type": "debugpy", "request": "launch",
"program": "${file}", "console": "integratedTerminal",
"python": "/ABSOLUTE/PATH/TO/.venv/bin/python",
"cwd": "${workspaceFolder}"
}]
}
After creating either file, run Developer: Reload Window (Cmd/Ctrl+Shift+P) so VS Code re-reads it.The green ▷ Run button uses VS Code's selected interpreter, which can differ from the one your terminal activated. This optional file forces the Run/Debug button onto your venv so both agree.
"name"is just the label you'll see in the Run menu — call it anything."program": "${file}"means "run whichever file is currently open".${file}is a VS Code variable it fills in for you."python"is the important line: point it at your venv's Python (the same/ABSOLUTE/PATH/TO/.venv/bin/python) so the button never uses a different one."cwd": "${workspaceFolder}"runs the file from the folder you opened, so relative paths behave like they do in the terminal.
Try this: Reload the window, then click ▷. If a script that worked in the terminal now also works from the button, this file did its job.
.code-workspace file (a multi-folder workspace) instead of a single folder, its "settings" block overrides every folder's .vscode/settings.json. Symptom: you set the interpreter per-folder but VS Code keeps auto-selecting some other venv it found in a sibling folder (you'll see its name, e.g. (some-old-env), in the terminal). Fix: put the Python settings in the workspace file itself:
Your.code-workspace{
"folders": [ /* ... */ ],
"settings": {
"python.defaultInterpreterPath": "/ABSOLUTE/PATH/TO/.venv/bin/python",
"python.terminal.activateEnvironment": false
}
}
To find out if you're in a workspace: the Explorer title shows "WORKSPACE" and File menu has "Save Workspace As". If you just want one folder, use File → Open Folder instead of opening a .code-workspace.A multi-root workspace is a single .code-workspace file that groups several folders. Its own "settings" block beats every folder's .vscode/settings.json — which is why a per-folder interpreter can be silently ignored. The fix is to set Python here instead.
"folders"lists the folders in the workspace — the/* ... */is just a placeholder comment for whatever's already there; leave it as-is.- The
"settings"block is the override. Putting"python.defaultInterpreterPath"here makes it win over any folder-level file. "python.terminal.activateEnvironment": falseis set tofalseon purpose here — in a workspace you often let the shell hook (section 10) handle activation instead, avoiding a fight between the two.
Try this: Not sure you're even in a workspace? The Explorer title shows "WORKSPACE" and the File menu offers "Save Workspace As". If so, edit this file, not the folder's.
10 · Auto-activate the venv by folder (macOS/Linux, zsh)
Here's the most convenient setup: make the venv activate automatically whenever any terminal enters your project folder — in VS Code and the system Terminal — with nothing to type. This is a shell hook, added once to ~/.zshrc. It's more reliable than VS Code's auto-activation because it targets one specific venv and folder, so a stale environment can never sneak back in.
Open ~/.zshrc (code ~/.zshrc) and paste this at the bottom. Replace the two paths with your project folder and your venv:
~/.zshrc# --- auto-activate the course venv inside my project folder ---
my_course_venv() {
local proj="/ABSOLUTE/PATH/TO/your-project-folder"
local venv="/ABSOLUTE/PATH/TO/.venv"
if [[ "$PWD" == "$proj"* ]]; then
[[ "$VIRTUAL_ENV" != "$venv" && -f "$venv/bin/activate" ]] && source "$venv/bin/activate"
elif [[ -n "$VIRTUAL_ENV" && "$VIRTUAL_ENV" == "$venv" ]]; then
deactivate 2>/dev/null
fi
}
autoload -Uz add-zsh-hook
add-zsh-hook chpwd my_course_venv # run on every directory change
my_course_venv # and once when the shell starts
# --- end ---
This is a small shell function you paste once into ~/.zshrc (the file zsh runs at startup). It makes your venv switch on the moment any terminal enters your project folder, and off when you leave — with nothing to type. macOS/Linux only.
my_course_venv()defines the function. The twolocallines setproj(your project folder) andvenv(your venv) — replace both placeholder paths with your real ones.- The
if [[ "$PWD" == "$proj"* ]]test asks "is the current folder ($PWD) inside my project?" The trailing*means subfolders count too. If yes and the venv isn't already on, it runssource …/activate. - The
elifbranch handles leaving: if you've stepped out of the project but the course venv is still on, it runsdeactivateto turn it off. add-zsh-hook chpwd my_course_venvtells zsh to call the function on every directory change (chpwd), and the baremy_course_venvline runs it once when the shell first opens.
What the output means: Enter the folder → prompt shows (.venv); leave it → the prefix disappears. It behaves the same in VS Code's terminal, Terminal.app, and iTerm, because they all read ~/.zshrc.
Try this: To use it in a terminal you already have open (without restarting), run source ~/.zshrc, then cd into and out of your project and watch the prompt change.
(.venv). Leave it → it deactivates. It works in every zsh terminal on the machine (VS Code, Terminal.app, iTerm) because ~/.zshrc is read by them all. To apply it to your current terminal without reopening: source ~/.zshrc. On Windows, this shell hook doesn't apply — rely on the .vscode/settings.json from §9 instead, or activate manually..venv there — thousands of package files will thrash the sync and can corrupt the environment. Keep code in the synced folder and the venv in a local path (e.g. your home directory), and point the settings/hook at that local venv. That's the split this course's author uses.11 · Verify the whole setup
Create this one-line check in your project folder and run it — it confirms the SDK imports in the environment VS Code is using, and needs no API key.
<your-project>/check_setup.py
check_setup.pyfrom anthropic import Anthropic
import anthropic, sys
print("anthropic version:", anthropic.__version__)
print("python:", sys.executable)
print("import OK — you can build the labs here.")
terminalpython check_setup.py
anthropic version: 1.x.x
python: /ABSOLUTE/PATH/TO/.venv/bin/python
import OK — you can build the labs here.
This tiny script is a health check: it proves the Anthropic library is installed in the exact Python that VS Code and your terminal are using. It needs no API key — it only imports and prints, it doesn't call the API.
from anthropic import Anthropicandimport anthropicload the library. If the venv is wrong or the install failed, this line errors immediately withModuleNotFoundError— which is itself a useful signal.print("anthropic version:", anthropic.__version__)shows which version got installed.print("python:", sys.executable)prints the full path of the Python actually running the script — this is the line that tells you whether you're in the right venv.- Run it with
python check_setup.pyfrom your activated venv.
What the output means: A version number, a python: path that should end in .venv/bin/python, and import OK. If the path is a system Python (like /usr/bin), the wrong interpreter is active — revisit sections 4 and 9.
Try this: Deliberately run it without activating the venv. You'll likely get ModuleNotFoundError or a system python: path — a clear picture of what "wrong environment" looks like.
python: path is your .venv and you see import OK, your environment is correct — any remaining red underline in the editor is just a stale cache, cleared by Developer: Reload Window. If the path is a system Python (e.g. /usr/bin or /opt/homebrew), the wrong interpreter is active — revisit §4 (activate) and §9 (settings).Troubleshooting — VS Code specifics
| What you see | What it means & the fix |
|---|---|
(agents) or another env auto-appears in the terminal | VS Code selected that interpreter, or your shell profile activates it. Run Python: Select Interpreter → pick your .venv; or conda deactivate / deactivate. |
ModuleNotFoundError right after installing | The venv isn't active (no (.venv) in prompt) or you're in the wrong folder. Activate it and run from the project folder. |
| The ▷ Run button uses the wrong Python | Python: Select Interpreter and choose the .venv one; reopen the terminal. |
command not found: python3 | Python isn't installed or not on PATH. Install from python.org; on Windows use py. |
| Terminal opens in the wrong directory | You opened a parent folder. Open Folder on the exact project folder, or cd into it. |
| Activate script blocked on Windows | PowerShell execution policy — run Set-ExecutionPolicy -Scope CurrentUser RemoteSigned once, then re-activate. |
| You set the interpreter per-folder but VS Code keeps picking a different venv | You have a .code-workspace open — its settings override folder settings. Put the interpreter in the workspace file's "settings" block (§9 danger note), or open a single folder instead. |
A stale env (e.g. (agents)) auto-activates and its python is "command not found" | VS Code auto-selected a broken/old venv it found in a parent folder. Set the correct interpreter in the workspace/folder settings and set python.terminal.activateEnvironment appropriately; that env's Python was moved/deleted, so stop pointing at it. |
| Import works in the terminal but the ▷ Run button fails | The button uses the selected interpreter, not the terminal's venv. Add the launch.json from §9, or select the venv interpreter — then Reload Window. |
| Two folders with the same name (one has the venv, one doesn't) | Confirm which folder VS Code actually has open (Explorer title). Put the code + .vscode config in the folder you truly open, and point it at wherever the .venv really lives. |
| Red underline persists after fixing the interpreter | Stale Pylance cache — Developer: Reload Window. Confirm with §11: if check_setup.py prints import OK, the code is fine. |
✓ You're ready for the course when…
- VS Code, the Python extension, and the Anthropic extension are installed.
- You can open a folder, open the terminal, and see it start in that folder.
- You can create a venv, activate it, and see
(.venv)in your prompt. - You can run
python file.pyandpython -m pytest tests/ -v. - Your API key is set as an environment variable, never in code.
- You've asked the Anthropic extension to explain a file.
- (Optional) A
.vscode/settings.jsonpins the interpreter, and/or a~/.zshrchook auto-activates the venv by folder. check_setup.pyprintsimport OKwith your.venvpath (§11).