Running Python Notebooks#
This guide shows how to work with Jupyter notebooks in VS Code, the primary way you’ll run Python code in this course. For initial setup, see the Python Installation Guide.
Opening Your Assignment Repository#
After accepting a GitHub Classroom assignment:
Clone your repository (if you haven’t already):
Copy the repository URL from GitHub
Open VS Code
Press
Ctrl+Shift+P(orCmd+Shift+Pon Mac) to open the command paletteType “Git: Clone” and select it
Paste your repository URL and choose a folder to save it
Open the cloned repository in VS Code:
File → Open Folder
Navigate to and select your cloned assignment folder
Click “Open”
Your repository should now be open in VS Code with all assignment files visible in the Explorer sidebar.
Opening and Running Jupyter Notebooks#
Step 1: Open a Notebook File#
In VS Code Explorer, find the
.ipynbfile (usually namedassignment.ipynbor similar)Click on it to open it
The notebook will open in VS Code’s built-in notebook editor
Step 2: Select Your Python Kernel#
Important: You must select the correct Python environment (kernel) before running code.
Look for “Select Kernel” in the top-right corner of the notebook (it might say “Python 3.xx.x” or “Select Kernel”)
Click on it
From the dropdown, select your virtual environment:
It should appear as “Python (.venv)” or similar
If you don’t see it, click “Python Environments” → “Refresh” or restart VS Code
If your environment doesn’t appear:
# In VS Code terminal (Ctrl+` or Cmd+`)
pip install ipykernel
python -m ipykernel install --user --name=.venv --display-name "Python (.venv)"
Then restart VS Code and try selecting the kernel again.
Step 3: Run Notebook Cells#
Run a single cell:
Click the play button (▶️) next to the cell
Or press
Shift+Enterwhile your cursor is in the cell
Run all cells:
Use the “Run All” button in the toolbar at the top
Or press
Ctrl+Shift+P→ “Notebook: Run All Cells”
Run cells above/below current cell:
Right-click in a cell and choose “Run Above” or “Run Below”
Step 4: Working with Cell Outputs#
Plots and visualizations will appear directly below the code cell
Text output (like
print()statements) appears below the cellErrors will be displayed with red text and stack traces
Save your work frequently with
Ctrl+S(orCmd+S)
Troubleshooting Notebook Issues#
Kernel Not Connecting#
Make sure your virtual environment is activated in the VS Code terminal
Try restarting VS Code
Check that ipykernel is installed:
pip install ipykernel
Cells Not Running#
Ensure you’ve selected the correct kernel (see Step 2 above)
Check for error messages in the cell output
Try restarting the kernel: Click the kernel name → “Restart Kernel”
Package Import Errors#
Make sure you’ve installed all requirements:
pip install -r requirements.txtCheck that you’re using the correct virtual environment
Some packages need system libraries - see Python Installation Guide troubleshooting
VS Code Notebook Interface Tips#
Add new cells: Click the “+” button or use
Ctrl+Shift+P→ “Notebook: Insert Cell Below”Change cell type: Use the dropdown in the bottom-right of each cell (Code/Markdown)
Clear outputs: Right-click a cell → “Clear Cell Outputs”
Variable explorer: View variables in the Jupyter panel (beaker icon in left sidebar)
Debug cells: Use the debugger by clicking the bug icon next to the run button
Alternative: Command Line Jupyter#
If you prefer the traditional Jupyter web interface:
Open VS Code terminal:
Ctrl+`` (orCmd+``)Activate environment:
source .venv/bin/activate(Linux/Mac) or.venv\Scripts\Activate.ps1(Windows)Start Jupyter:
jupyter labOpen notebooks in your web browser
Note: VS Code provides better integration, so we recommend using the built-in notebook editor.