2025, Nov 18 05:00

How to Fix 'Javascript Error: IPython is not defined' in Jupyter Notebook with ipywidgets and Matplotlib

Fix 'Javascript Error: IPython is not defined' in Jupyter Notebook 7. Enable widgetsnbextension and ipympl, use %matplotlib widget to restore interactive plots

Jupyter Notebook opens, cells execute, but any interactive output fails with a pink banner that says one thing and one thing only: Javascript Error: IPython is not defined. If you rely on ipywidgets or the Matplotlib interactive backends, that message stops your workflow cold. Here is a concise walkthrough of what triggers it in this setup and how to get back to working widgets and live plots.

Reproducing the issue

The failure shows up immediately with interactive backends. For example, running the following minimal cell in a fresh notebook leads to the error banner:

%matplotlib nbagg
import matplotlib.pyplot as graph
canvas, area = graph.subplots()

The environment uses a recent Miniconda base, Jupyter Notebook, and packages such as ipython, matplotlib, and ipympl installed into the default base environment. The kernel is Python 3 (ipykernel).

What is actually going wrong

The key symptom is that the browser front end cannot find IPython on the JavaScript side. In this situation, the corresponding front-end assets that power widgets and interactive Matplotlib do not load, so the notebook interface reports that the IPython object is unavailable. In practice, this happens when the necessary nbextensions are not enabled for the environment you launched Jupyter from or when the commands to wire them up were run outside the active environment. One telltale sign was hitting an error like jupyter-nbextension not found until the correct environment was activated.

There is also a practical nuance observed here with Jupyter Notebook 7. Installing inside the notebook with the magic helped only when switching to the modern interactive backend. Specifically, using %pip install ipython allowed plots to appear with %matplotlib widget, while the deprecated nbagg continued to fail.

Fix that restores interactive output

Start by ensuring you are operating in the same environment where you installed the packages. Activate the intended environment explicitly and enable the required extensions. Then restart and fully reload everything before re-running the notebook.

conda activate base
jupyter nbextension enable --py widgetsnbextension
jupyter nbextension enable --py ipympl

Restart your computer after this step. Then launch the notebook interface again from the same environment.

jupyter notebook

Inside the notebook, use the menu to restart the kernel, clear all output, and then run all cells. If the browser still holds onto stale assets, a hard refresh of the page can finalize the propagation. On Windows this can be done with Ctrl+Shift+R or Ctrl+F5, and on macOS in Chrome with Command+Shift+R.

Updated code to verify

If you prefer the modern interactive path that worked in Notebook 7 during troubleshooting, use the widget backend. The core logic is the same; only names differ from the earlier snippet.

%matplotlib widget
import matplotlib.pyplot as painter
frame, region = painter.subplots()

At this point, ipywidgets and interactive Matplotlib should render. If you specifically require nbagg, enabling the extensions as shown above addresses the missing front-end wiring that led to the original error.

Why this matters

Interactive computing in Jupyter depends on Python and JavaScript components being correctly registered in the same environment from which the notebook server is launched. When any part of that chain is not active, the browser UI cannot load the expected objects and surfaces generic messages. Making sure extensions are enabled in the actual runtime environment eliminates the silent mismatch and brings back the expected interactivity.

Practical notes for installations from the notebook

When installing packages from inside Jupyter, prefer the built-in magic. In this case, using %pip install inside Notebook 7 helped bring up interactive plots with %matplotlib widget. Always include the complete error text when troubleshooting because even sparse banners sometimes vary and can hint at where the breakage occurs.

Conclusion

When a notebook shows Javascript Error: IPython is not defined, verify that you launched Jupyter from the intended environment, enable the widgets and ipympl extensions in that environment, restart everything, and then run all cells. If you are working in Notebook 7 and want an interactive backend that cooperates with the modern stack, switch to %matplotlib widget. These steps align the Python and JavaScript sides of the stack and restore the interactive features you expect.