This issue happens because Jupyter uses a different Python environment from the one where you installed mypkg in editable mode. Although running
%pip install -e /path/to/mypkg inside the notebook prints a success message, that command may run in a separate interpreter than the one your active notebook kernel uses.
When you later execute !python -m mypkg.module, it works only because !python launches an external process that uses your system or virtualenv Python — not the running Jupyter kernel’s Python session.
Confirm what’s happening
To verify the mismatch:
import sys
print(sys.executable)
!which python
!jupyter kernelspec list
If these point to different paths, that’s the cause of your ModuleNotFoundError.
Solutions
1. Ensure your notebook kernel uses the same environment
Activate the same environment you used for the editable install and register it as a Jupyter kernel:
source /path/to/venv/bin/activate
pip install ipykernel
python -m ipykernel install --user --name mypkg-env --display-name "mypkg env"
Then reopen your notebook and select “mypkg env” from the kernel list.
2. Verify installation path
In a notebook cell, print out:
import site
site.getsitepackages()
Check that the editable-path .egg-link file for mypkg is in one of these directories.
3. As a fallback, add it to sys.path manually
If switching kernels is not possible, append the install location:
import sys
sys.path.append("/path/to/mypkg")
import mypkg.module
This mirrors what happens automatically in the terminal’s sys.path but not in Jupyter’s environment.
4. Optionally reinstall inside the notebook’s environment
If you confirm that the notebook uses a different environment (e.g., Conda base or system Python), reinstall mypkg inside it:
%pip install -e /path/to/mypkg --force-reinstall
Once the kernel and Jupyter runtime point to the same Python interpreter where the editable installation resides, import mypkg.module will succeed consistently.