4. Run a Notebook in VS Code

Duration1h AI Banned

Introduction

A notebook is executed by a Python process called a kernel. Opening an .ipynb file in VS Code does not guarantee that it uses your project environment: you must select the Python interpreter created by uv and verify it before beginning the analysis. This activity also establishes a reliable way to diagnose environment and file-path problems.

Download the Sales resources

Download the notebook and save it as notebooks/sales.ipynb inside your project:

Download Sales Notebook

Download the dataset and save it as data/sales.csv:

Download Sales Dataset

Your project should now contain:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
sales-analysis/
├── .python-version
├── .venv/
├── README.md
├── data/
│   └── sales.csv
├── notebooks/
│   ├── images/
│   └── sales.ipynb
├── pyproject.toml
└── uv.lock

Verify the filenames in the VS Code Explorer. Browser downloads sometimes add an extension or retain the name sales_todo.ipynb; rename the files when necessary.

Open the notebook from the course workspace

Keep the data-science-toolkit workspace created in Activity 2 open in VS Code and select sales-analysis/notebooks/sales.ipynb from the Explorer. Windows users must also verify that the window is connected to WSL.

Select the uv environment

  1. Click Select Kernel in the upper-right corner of the notebook.
  2. Select Python Environments.
  3. If it is listed, choose the interpreter located in sales-analysis/.venv.
  4. If it is not listed, follow Add the Sales interpreter path manually.
  5. Wait for the kernel to start.

If several interpreters have similar names, use the path rather than the Python version to identify the correct one. Under Linux and WSL, the expected path ends with:

1
sales-analysis/.venv/bin/python

Because sales-analysis is a project inside a larger workspace, VS Code may not discover its nested .venv automatically. It may instead suggest another Python environment located at the workspace root. This is expected: use the complete interpreter path to identify the Sales environment. If sales-analysis/.venv is not displayed in the kernel list, go directly to Add the Sales interpreter path manually.

Windows users

Do not select an interpreter whose path starts with C:\. Your notebook, uv environment, and Python kernel must all run inside WSL.

Verify the active kernel

Create a temporary code cell at the top of the notebook and run:

1
2
3
4
5
import sys
from pathlib import Path

print("Python executable:", sys.executable)
print("Working directory:", Path.cwd())

The Python executable must point to .venv. The working directory tells you how relative file paths will be interpreted.

Test the analysis libraries in a second cell:

1
2
3
4
5
6
7
import matplotlib
import numpy
import pandas
import scipy
import seaborn

print("Environment ready")
Checkpoint 1

Both cells run without error, and sys.executable points to sales-analysis/.venv/bin/python.

Load the Sales dataset

The provided notebook is stored in notebooks/, while the dataset is stored in data/. From the notebook directory, the relative path is therefore:

1
2
3
4
5
from pathlib import Path

data_path = Path("../data/sales.csv")
print(data_path.resolve())
print(data_path.exists())

When the final line displays True, load the first rows:

1
2
3
4
import pandas as pd

sales = pd.read_csv(data_path)
sales.head()

If the file is not found, inspect the working directory printed earlier instead of replacing the path with an absolute path from your computer.

Checkpoint 2

The notebook uses the .venv kernel, data_path.exists() returns True, and pandas displays the first rows of the Sales dataset.

Troubleshooting

Add the Sales interpreter path manually

VS Code often detects a .venv at the workspace root but does not automatically search every nested project. First, obtain the exact path of the Python interpreter managed by uv:

1
2
3
cd ~/data-science-toolkit/sales-analysis
uv sync --locked
uv run python -c "import sys; print(sys.executable)"

Copy the displayed path. It should end with sales-analysis/.venv/bin/python. Then:

  1. Open the Command Palette with Ctrl+Shift+P.
  2. Select Python: Select Interpreter.
  3. Select Enter interpreter path.
  4. Paste or browse to the path returned by uv.
  5. Open the Command Palette again and select Developer: Reload Window.
  6. Return to the notebook and choose Select Kernel → Python Environments.
  7. Select the Sales interpreter that you just added.
Why is another .venv suggested?

The environment closest to the workspace root is easier for VS Code to discover. It may belong to another project and must not be used merely because it appears first. Always identify a kernel by its complete path.

The Sales interpreter exists but ipykernel is unavailable

Run the following commands from sales-analysis:

1
2
uv sync --locked
uv run python -c "import ipykernel; print('ipykernel available')"

If the import succeeds, reload the VS Code window and add the interpreter path manually as described above.

VS Code proposes to install ipykernel

Do not install it globally. Run uv add --dev ipykernel from the project root, reload the VS Code window, and select .venv again.

The notebook uses the wrong Python

Compare the notebook output with the terminal output:

1
uv run python -c "import sys; print(sys.executable)"

Both paths must identify the same .venv. Select another kernel if they differ.

The dataset cannot be found

Check Path.cwd(), the spelling and capitalization of sales.csv, and the project tree in the VS Code Explorer. Do not use a machine-specific absolute path, because another student would not be able to reproduce it.

The kernel never starts under Windows

Check the WSL indicator, open a new Linux terminal, run uv sync --locked, and use Reopen Folder in WSL if the folder was opened locally.

Activity complete

You now have a notebook running locally in VS Code with the Python environment managed by uv. Keep the diagnostic cells until the first Git publication; they provide evidence that the project works correctly.