Use Claude Code for Python in VS Code on Mac
You’ve set up Python in VS Code and can write code manually. Now you want AI to help write, improve, and debug your Python code while you focus on the analysis. Think of Claude Code as a coding partner who lives inside VS Code - you describe what you want, and it writes or improves the code while you stay in the same window.
Key Concepts
- Claude Code Extension - VS Code extension that brings Claude AI assistant directly into your editor with inline code edits
- Chat Panel - Side panel in VS Code where you converse with Claude about your code
- Inline Edits - Claude’s suggested code changes appear directly in your files with diffs you can accept or reject
What You’ll Need
- Finished Python in VS Code tutorial - Python should be working in VS Code
- Finished Installing Claude Code on Mac tutorial - Claude Code CLI should be installed
- A paid Claude plan (such as Pro or Max) or an Anthropic Console account — the free plan doesn’t include Claude Code
- 20-30 minutes
Step 1: Open VS Code and Create Python Project
- Open Visual Studio Code
- Click File in the menu bar, then Open Folder
- Navigate to your Documents folder
- Click New Folder at the bottom of the dialog
- Name the folder
claude_python_test - Click Open to open the new folder
- If prompted “Do you trust the authors?”, click Yes, I trust the authors
Step 2: Install and Sign in to Claude Code Extension
- Click the Extensions icon in the left sidebar (four squares icon)
- In the search box, type
Claude Code - Find Claude Code for VS Code by Anthropic and click Install
- After installation, click ✻ Claude Code in the bottom-right corner of the VS Code window (the status bar) to open the Claude panel
- The first time, a sign-in screen appears: click Sign in, approve access in your browser, then return to VS Code
- For detailed authentication steps, see the Installing Claude Code on Mac guide (Step 3)
- Once signed in, you’ll see a Learn Claude Code checklist and a prompt box at the bottom of the panel
Step 3: Create Initial Python Script Manually
- Click File > New File… in the menu bar
- In the list that appears at the top of the window, choose Python File
- Save the file by clicking File > Save, name it
iris_analysis.py - Type this code:
from sklearn import datasets
import pandas as pd
# Load iris dataset
iris = datasets.load_iris()
iris_df = pd.DataFrame(iris.data, columns=iris.feature_names)
iris_df['species'] = iris.target_names[iris.target]
# Display structure
iris_df.info()
print("\nSummary statistics:")
print(iris_df.describe())
- Save the file by clicking File > Save
- Create a virtual environment, as in Step 5 of Python Coding in VS Code: click View > Command Palette, type
Python: Create Environment, choose Venv, then pick your Python version - Wait for the
.venvfolder to appear in the Explorer - Open a terminal: click View in the menu bar, then Terminal. You should see
(.venv)at the start of the prompt - Install the packages this script needs:
pip install scikit-learn pandas - In the terminal, run:
python iris_analysis.py - You should see dataset structure and summary statistics in the terminal
Step 4: Ask Claude to Add Scatter Plot
- Open the Claude panel (Spark icon at the top-right of the open file, or ✻ Claude Code in the bottom-right corner)
- In the chat box at the bottom, type:
Add code to iris_analysis.py to create a scatter plot of sepal length vs. width, colored by species. Use seaborn.
- Press Enter to send
- Claude reads your file and edits it - the new code appears in
iris_analysis.py - If Claude shows a diff and asks for permission, accept the change
- The seaborn/matplotlib code appears in your file
- Install the plotting packages Claude’s code uses (they weren’t installed in Step 3). In the terminal (with
(.venv)showing), run:pip install seaborn matplotlib
Step 5: Run the Updated Code
- In the terminal, run:
python iris_analysis.py - A scatter plot window appears
- You should see points colored by species (setosa, versicolor, virginica)
- Close the plot window to continue
Step 6: Ask Claude to Refine Plot
- In the Claude chat panel, type:
Remove title. Change marker type by species. Change to the whitegrid style.
- Press Enter
- Claude updates the code
- If asked, accept the change
- Run the updated code again:
python iris_analysis.pyin the terminal - The plot now displays with different marker shapes per species, no title, and whitegrid style
- Close the plot window to continue
Step 7: Ask Claude for PCA Analysis
- In the Claude chat panel, type:
Add code to perform PCA on the numeric variables and plot the samples using the first two principal components.
- Press Enter
- Claude adds PCA code to your script
- If asked, accept the change
- Run the code:
python iris_analysis.pyin the terminal - A PCA plot appears showing samples projected onto PC1 and PC2, colored by species (if the scatter plot window from Step 6 opens first, close it and the PCA plot appears next)
- Close the plot window when done
Step 8: Ask Claude to Review and Comment
- In the Claude chat panel, type:
Review the entire script for correctness. Add comments when necessary.
- Press Enter
- Claude reviews the code and adds comments explaining each section
- If asked, accept the change
- Your script now has clear comments explaining the data loading, visualization, and PCA analysis
Next Steps
- Ask Claude to create a Jupyter notebook: “Convert this to a Jupyter notebook”
- Use Claude to debug errors: “This code gives Error X, can you fix it?”
- Request statistical tests: “Add a t-test comparing sepal length between setosa and versicolor”
- Ask Claude to optimize code: “Make this code more efficient using numpy vectorization”
- Get help with unfamiliar functions: “Explain what pandas.groupby does”
Troubleshooting
- Claude extension not showing - Restart VS Code after installation. Click View > Extensions to verify it’s installed.
- “Cannot read Python file” error - Make sure you saved the file with
.pyextension. Claude needs saved files to read them. - Authentication failed - Verify your paid Claude plan or Anthropic Console account is active. See Installing Claude Code on Mac for authentication troubleshooting.
- Package not found errors or
command not found: python- Make sure the terminal shows(.venv)(if not, close it and open a new one with View > Terminal), then install the required packages:pip install seaborn matplotlib scikit-learn pandas. Wait for installation to complete before running code. - Code changes not applying - If Claude asked for permission, make sure you accepted the change. If it’s not working, try copying Claude’s suggested code manually.
- Plot not showing - Make sure you have a display. On some systems you may need to add
plt.show()at the end or use%matplotlib inlinein Jupyter.
Workflow Summary
- Claude Code Extension provides an AI assistant directly in VS Code with chat panel and inline diffs
- Iterative refinement - Start with basic code, ask Claude to enhance, test immediately, then refine further
- Context awareness - Claude reads your Python files and understands your project structure
- Inline editing - Proposed changes appear as diffs in your actual files, not separate chat responses
- Stay in flow - No switching between browser and editor - everything happens in VS Code
Created by Steven Ge on December 11, 2025.