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:

  1. Clone your repository (if you haven’t already):

    • Copy the repository URL from GitHub

    • Open VS Code

    • Press Ctrl+Shift+P (or Cmd+Shift+P on Mac) to open the command palette

    • Type “Git: Clone” and select it

    • Paste your repository URL and choose a folder to save it

  2. 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#

  1. In VS Code Explorer, find the .ipynb file (usually named assignment.ipynb or similar)

  2. Click on it to open it

  3. 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.

  1. Look for “Select Kernel” in the top-right corner of the notebook (it might say “Python 3.xx.x” or “Select Kernel”)

  2. Click on it

  3. 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#

  1. Run a single cell:

    • Click the play button (▶️) next to the cell

    • Or press Shift+Enter while your cursor is in the cell

  2. Run all cells:

    • Use the “Run All” button in the toolbar at the top

    • Or press Ctrl+Shift+P → “Notebook: Run All Cells”

  3. 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 cell

  • Errors will be displayed with red text and stack traces

  • Save your work frequently with Ctrl+S (or Cmd+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.txt

  • Check 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:

  1. Open VS Code terminal: Ctrl+`` (or Cmd+``)

  2. Activate environment: source .venv/bin/activate (Linux/Mac) or .venv\Scripts\Activate.ps1 (Windows)

  3. Start Jupyter: jupyter lab

  4. Open notebooks in your web browser

Note: VS Code provides better integration, so we recommend using the built-in notebook editor.