Resolving ModuleNotFoundError: No module named 'jupyter_server.contents'
Problem Statement
When launching Jupyter Notebook, users encounter this critical error preventing startup:
ModuleNotFoundError: No module named 'jupyter_server.contents'
Traceback (most recent call last):
...
TypeError: warn() missing 1 required keyword-only argument: 'stacklevel'
The error occurs due to version conflicts between Jupyter components, specifically:
- Missing dependencies in the notebook package
- Incompatible versions of the
traitlets
library - Version conflicts after recent package updates
The core issue originates from breaking changes in Jupyter ecosystem packages, particularly notebook
and traitlets
, creating a compatibility gap when the required module jupyter_server.contents
becomes inaccessible.
Solutions
Recommended Modern Fix (Post-2023)
Install a patched version of the notebook
package (v6.5.6 or newer):
# Install specific fixed version
pip install notebook==6.5.6
# Install latest compatible 6.x version
pip install --upgrade --no-cache-dir "notebook>=6.5.6,<7"
Why this works
Version 6.5.6+ contains compatibility fixes addressing the missing jupyter_server.contents
module. The --no-cache-dir
flag prevents installation of corrupted cached packages.
Legacy Workaround (For Older Environments)
If notebook upgrades aren't feasible, downgrade traitlets
:
pip uninstall traitlets
pip install traitlets==5.9.0
Caution
This should only be used temporarily, as locking traitlets
to old versions causes compatibility issues with other libraries.
Verification Steps
Confirm successful resolution:
# Check installed versions
pip show notebook traitlets
# Start Jupyter to verify
jupyter notebook
::: success Expected Output
Name: notebook
Version: 6.5.6
...
[I xx:xx:xx.xxx NotebookApp] Serving notebooks from local directory: ...
:::
Prevention Best Practices
Always pin dependencies in
requirements.txt
:txtnotebook==6.5.6 jupyter_server==2.*
Create isolated environments using virtualenv/conda:
bashpython -m venv jupyter-env source jupyter-env/bin/activate pip install notebook==6.5.6
Update judiciously using compatibility-tested versions:
bashpip install -U "notebook>=6.5.6,<7" "jupyter_server>=2,<3"
Version Guidance
Package | Safe Version Range | Unsafe Versions |
---|---|---|
notebook | >=6.5.6, <7 | 5.10.0, 6.0-6.5 |
traitlets | >=5.9.0, <6 | 5.10.0 |