Resolving ModuleNotFoundError for notebook.base When Installing Jupyter nbextensions
Problem Statement
When attempting to install Jupyter Notebook extensions (nbextensions) using jupyter contrib nbextension install --user
, you encounter a critical error:
ModuleNotFoundError: No module named 'notebook.base'
This error occurs despite following official installation steps:
- Installing Jupyter Notebook (
pip install notebook
) - Installing nbextensions (
pip install jupyter_contrib_nbextensions
) - Running the nbextensions installer (
jupyter contrib nbextension install --user
)
Common troubleshooting steps like reinstalling packages, clearing caches, or creating virtual environments fail to resolve the issue. The core problem stems from version incompatibilities between Jupyter Notebook components and the nbextensions package.
Root Cause
The error originates from recent architectural changes in Jupyter Notebook (version 7.0+). The notebook.base
module was removed in newer releases, breaking compatibility with the current version of jupyter_contrib_nbextensions
. Specifically:
- Newer
notebook
versions ≥7.0 lack thenotebook.base.handlers
module jupyter_contrib_nbextensions
hasn't been updated to accommodate these changes- Dependency conflicts between
traitlets
,ipython
, andnotebook
exacerbate the issue
Solutions
1. Downgrade Dependencies (Recommended)
Compatibility is restored by installing specific package versions:
pip uninstall notebook traitlets jupyter
pip install notebook==6.4.12 traitlets==5.9.0
pip install jupyter_contrib_nbextensions --no-deps
After installation, run the nbextensions installer:
jupyter contrib nbextension install --user
DEPENDENCY NOTES
If you encounter TypeError: warn() missing 1 required keyword-only argument: 'stacklevel'
, also downgrade ipython
:
pip uninstall ipython
pip install ipython==8.9.0
2. Conda Installation Method
For Anaconda/miniconda users, install from conda-forge
:
conda install -c conda-forge notebook jupyter_contrib_nbextensions
jupyter contrib nbextension install --user
CONDA TROUBLESHOOTING
If dependencies conflict, update your base environment first:
conda update --all
3. Virtual Environment Setup
Isolate compatible packages using a virtual environment:
python -m venv jupyter_env
source jupyter_env/bin/activate # Linux/macOS
jupyter_env\Scripts\activate # Windows
pip install notebook==6.4.12 traitlets==5.9.0 jupyter_contrib_nbextensions
VERSION COMPATIBILITY
The tested working configuration uses:
notebook==6.4.12
traitlets==5.9.0
jupyter_contrib_nbextensions==0.7.0
Alternative Code Formatting Extensions
Since the initial query mentions alternative code formatters, consider these nbextensions compatible with Notebook 6.4.12:
Code Prettify
Install via nbextensions configurator:bashjupyter nbextension enable code_prettify/code_prettify
Autopep8 Extension
Install and activate:bashpip install autopep8 jupyter nbextension enable autopep8/main
jupyter-black
Install and enable:bashpip install jupyter-black jupyter nbextension enable jupyter-black-master/jupyter-black
Verification
Confirm successful installation:
jupyter nbextension list
Expected output should include:
jupyter-black-master/jupyter-black enabled
- Validating: OK
code_prettify/code_prettify enabled
- Validating: OK