Sub topic 6
Register the environment as a kernel
This chapter assumes you can create and activate a virtual environment, install packages with pip, and start JupyterLab from a terminal. Those steps come from the earlier chapters. Here you will learn what a kernel is, how to register your project environment as one, and how to diagnose the most common mismatch: a notebook that runs a different Python than the one you installed your packages into.
What a kernel is
A notebook file (.ipynb) stores code and results. It does not run anything. When you execute a cell, JupyterLab sends the code to a separate process called a kernel. The kernel runs the code, keeps your variables in memory, and sends results back to the interface.
For Python, that process is an ordinary Python interpreter running a small program called ipykernel. Which interpreter it uses decides which packages you can import. This is why kernels matter for environment isolation: a virtual environment only helps you if the notebook actually runs inside it.
The JupyterLab server and the kernel are two different things. They can use different Python installations.
Why the default kernel can mislead you
If you installed JupyterLab inside your virtual environment and launched it from there, the default python3 kernel often points at that same environment. In that case everything works without extra steps.
Problems appear in these situations:
- JupyterLab is installed once globally or in one environment, and you want to use it with several project environments.
- You activated a different environment than the one that holds JupyterLab.
- A kernel named
python3was registered earlier by a different Python installation.
In each case, the notebook runs a Python that lacks your packages. The classic symptom is that pip install pandas succeeds in the terminal, yet import pandas fails in the notebook with ModuleNotFoundError.
Register the environment
Registering writes a small kernel specification (a "kernelspec") that tells Jupyter which interpreter to launch and what name to show in the picker. Activate your project environment first, so that python refers to the interpreter you want to register.
python -m pip install ipykernel
python -m ipykernel install --user --name data-analysis --display-name "Python (data-analysis)"ipykernel is normally installed as a dependency of JupyterLab, so the first line is often a no-op. Keeping it makes the instructions work when you set up a smaller environment that does not include JupyterLab.
The second command is the one from this tutorial's plan. Its options mean:
| Option | Meaning |
|---|---|
| `--user` | Install the kernelspec in your user directory, so no administrator rights are needed. |
| `--name data-analysis` | The internal identifier. It becomes a directory name, so use lowercase letters, digits, hyphens, and underscores. |
| `--display-name "Python (data-analysis)"` | The label shown in the kernel picker. It can contain spaces and parentheses. |
Use python -m ipykernel rather than a bare ipykernel command. The -m form guarantees that the interpreter running the command is the one that gets recorded, which is the whole point.
Check what was registered
List the kernels Jupyter knows about:
jupyter kernelspec listThe output shows each kernel name with the directory that holds its specification. Open the kernel.json file in the data-analysis directory and you will see something like this:
{
"argv": [
"/home/you/projects/sales/.venv/bin/python",
"-m",
"ipykernel_launcher",
"-f",
"{connection_file}"
],
"display_name": "Python (data-analysis)",
"language": "python"
}The first entry in argv is the absolute path to the interpreter inside your virtual environment. That path is what ties the kernel to the environment. Two consequences follow:
- If you move or rename the project folder, the recorded path breaks and the kernel fails to start. Remove and re-register it.
- The kernel keeps working even when the environment is not activated in the terminal that launched JupyterLab, because it uses the full path directly.
Select the kernel in a notebook
Start JupyterLab with jupyter lab. There are three ways to choose the kernel.
- On the Launcher tab, click the tile named "Python (data-analysis)" under Notebook. The new notebook starts with that kernel.
- In an open notebook, click the kernel name at the top right of the notebook toolbar, then pick a different kernel from the dialog.
- Use the Kernel menu and choose Change Kernel.
The kernel name is saved in the notebook's metadata. When you reopen the file, Jupyter looks for a kernelspec with the same name. If it does not exist on the current machine, you are prompted to pick another one. This is relevant when you share notebooks: a colleague must register a kernel with the same name, or choose their own.
Verify the kernel is the right one
Never assume. Run this in the first cell of a new notebook:
import sys
print(sys.executable)
print(sys.prefix)The printed path should point inside your project's .venv directory. If it points to a system location such as /usr/bin/python3, the notebook is not using your environment.
You can also confirm that packages resolve from the right place:
import pandas as pd
print(pd.__version__)
print(pd.__file__)The file path of pandas should live under the environment's site-packages directory.
A related check in a notebook cell is %pip. This magic installs into the kernel's own environment, unlike !pip, which runs whichever pip happens to be first on the shell's PATH.
%pip install seabornAfter installing a package with %pip, you may need to restart the kernel before importing it, particularly if you had already imported an older version.
Troubleshooting kernel problems
| Symptom | Likely cause | Fix |
|---|---|---|
| `ModuleNotFoundError` for a package you installed | The notebook uses a different kernel than the environment you installed into. | Print `sys.executable`, then switch or register the correct kernel. |
| Kernel list is missing your environment | The kernelspec was never registered, or was registered from a different environment. | Activate the environment and run the `ipykernel install` command again. |
| "Kernel failed to start" after moving a project | The path in `kernel.json` is outdated. | Remove the kernelspec and register it again. |
| Two kernels with confusing names | Re-registering with the same `--name` overwrites; different names create duplicates. | Use one clear name per project and remove old ones. |
| Notebook says "No such kernel named ..." | The notebook was created on another machine with a different kernel name. | Pick an existing kernel, or register one with the expected name. |
To remove a kernel you no longer need:
jupyter kernelspec uninstall data-analysisThis deletes only the specification. It does not delete the virtual environment or your notebooks.
Decision guide: one JupyterLab or one per environment
There are two workable patterns. Choose based on how many projects you run.
- JupyterLab in every project environment. Each project is self-contained, and the default kernel is correct. The cost is disk space and repeated installs. This is the simplest option and fits this tutorial.
- One JupyterLab, many registered kernels. You keep a single JupyterLab installation and register each project's environment as a kernel. This saves space and gives one interface. The cost is that each environment needs
ipykernel, and the registered paths are fragile if you move folders.
Either way, the rule is the same: the kernel's interpreter, not the server's, determines what your code can import.
Restarting and interrupting
Changing the kernel starts a fresh process, so all variables are lost. A few related controls are worth knowing:
- Interrupt stops a running cell without clearing variables. Use it for a cell that takes too long.
- Restart Kernel kills the process and starts a new one with the same interpreter. Memory is cleared, but the notebook's text and outputs stay.
- Shut Down Kernel ends the process and leaves the notebook without one until you pick again.
Restarting is also the correct response after you upgrade or reinstall a package the notebook has already imported. Python caches imported modules, so the old version stays in memory until the kernel restarts.
Exercise
You activated .venv, ran python -m pip install pandas, and started JupyterLab. In a notebook using the "Python 3 (ipykernel)" kernel, import pandas raises ModuleNotFoundError. Which cell tells you which interpreter the notebook is using, and what command do you run in the activated environment to fix it?
Show answer
Run import sys; print(sys.executable) in a cell. If the path is outside .venv, activate the environment and run python -m ipykernel install --user --name data-analysis --display-name "Python (data-analysis)". Then choose "Python (data-analysis)" in the notebook's kernel picker.
Exercise
Why does the kernel keep working when you launch JupyterLab from a terminal where the virtual environment is not activated?
Show answer
The kernel.json file stores the absolute path to the environment's Python interpreter in its argv list. The kernel launches that exact interpreter regardless of the terminal's PATH or activation state.