Sub topic 6
Register the environment as a kernel
This chapter assumes you have a project virtual environment with JupyterLab, pandas, and matplotlib installed, and that you can run jupyter lab from a terminal. You should be comfortable activating and deactivating a virtual environment.
What a kernel is
A kernel is the separate process that runs your code. The JupyterLab server and browser interface only display notebooks and pass cell contents to the kernel. The kernel executes the code and returns the results.
Each kernel is described by a kernelspec. This is a small directory holding a kernel.json file that records which Python executable to launch. When you choose a kernel in the picker, JupyterLab reads its kernelspec and starts that interpreter.
This is why installing a package is not enough. If JupyterLab starts a kernel from a different Python than the one where you ran pip install, import pandas fails with ModuleNotFoundError, even though the package is installed in your project.
Register the environment
Activate the project environment first. Then register it:
python -m ipykernel install --user --name data-analysis --display-name "Python (data-analysis)"The options have different jobs:
--nameis the internal identifier. It becomes the kernelspec directory name, so use lowercase letters, digits, and hyphens.--display-nameis the label shown in the kernel picker. It can contain spaces and parentheses.--userinstalls the kernelspec in your user account's Jupyter data directory instead of system-wide, so you don't need administrator rights.
The command uses python -m ipykernel, so the kernelspec records the interpreter of the environment you activated. Running it with the wrong environment active registers the wrong Python.
If ipykernel is missing, install it with python -m pip install ipykernel. Installing jupyterlab normally brings it in.
Check what is registered
List the kernelspecs Jupyter knows about:
jupyter kernelspec listYou should see data-analysis with a path under your user Jupyter directory. Open the kernel.json file in that directory. Its argv list starts with the full path to your environment's Python. If that path points somewhere unexpected, remove the kernel with jupyter kernelspec remove data-analysis and register it again from the correct environment.
Select the kernel in a notebook
- Start JupyterLab with
jupyter lab. - Create a new notebook from the Launcher. Choose Python (data-analysis).
- For an existing notebook, use Kernel > Change Kernel... and pick the same entry. The kernel name also appears at the top right of the notebook.
Then confirm which interpreter the notebook actually uses:
import sys
print(sys.executable)The output should end in something like .venv/bin/python (or .venv\Scripts\python.exe on Windows). If it shows a different path, the notebook is using another kernel.
Common errors
| Symptom | Likely cause | Fix |
|---|---|---|
| `ModuleNotFoundError` for a package you installed | Notebook uses a different kernel than the environment you installed into | Change the kernel and check `sys.executable` |
| New kernel is missing from the picker | Registration ran in the wrong environment, or the page was not refreshed | Run `jupyter kernelspec list`, then reload the browser tab |
| Kernel shows as "Python 3 (ipykernel)" only | You never registered the environment | Run the registration command with the environment active |
| Kernel fails to start after you moved or deleted `.venv` | `kernel.json` points to a path that no longer exists | Remove the kernelspec and register it again |
Exercise
You installed pandas into .venv, but a notebook raises ModuleNotFoundError: No module named 'pandas'. Name two checks you can run to find the cause, and the fix.
Show answer
Run import sys; print(sys.executable) in the notebook to see which Python the kernel uses, and run jupyter kernelspec list to see which interpreters the registered kernels point to. If the notebook is not using the .venv Python, select Python (data-analysis) through Kernel > Change Kernel. If that kernel is missing, activate .venv and run python -m ipykernel install --user --name data-analysis --display-name "Python (data-analysis)".