Python finds module, Jupyter Notebook does not – environment mismatch explained
In a terminal, python imports your package just fine. In Jupyter, the same import gives
ModuleNotFoundError. This usually means Jupyter is using a different Python environment from your
terminal. Here’s how to fix it.
1. Confirm which Python Jupyter is using
Run this in a notebook cell:
import sys print(sys.executable)
Then in your terminal:
python -c "import sys; print(sys.executable)"
Compare the two paths. If they’re different, they’re different environments.
2. Install the package in the notebook’s environment
If Jupyter uses a different interpreter, install your package from inside the notebook:
!{sys.executable} -m pip install your-package-name
This ensures it installs into the same environment the kernel is running.
3. Use virtual environments consistently
Create and activate a dedicated environment:
python -m venv .venv source .venv/bin/activate # on Windows: .venv\Scripts\activate pip install notebook
Then start Jupyter from that environment:
python -m notebook
Now your terminal and notebook share the same site-packages.
4. Register your environment as a Jupyter kernel
To make a specific env selectable in Jupyter’s kernel list:
python -m pip install ipykernel python -m ipykernel install --user --name myenv --display-name "Python (myenv)"
Then switch to it via Kernel → Change kernel → “Python (myenv)”.
5. Capture a stable environment snapshot with NoteCapsule
Once imports work in your notebook, freeze that environment and structure with a Capsule so you or your teammates can reproduce it later.
from notebookcapsule import create_capsule
create_capsule(
name="env-fixed-notebook",
notebook_path="notebook.ipynb",
data_dirs=["./data"],
base_dir=".", # project root
)
The Capsule includes a suggested requirements file and environment metadata tied to the working interpreter.
One environment, one command, fewer surprises
NoteCapsule helps you record which Python environment and dependencies actually worked with your notebook, so you don’t have to rediscover them on a different machine later.
Join NoteCapsule early access