Fix JavaScript Error: IPython is not defined in Jupyter

Deepak Prasad

In a Jupyter notebook browser tab, the console may show:

text
Javascript Error: IPython is not defined

That message usually means the notebook front end and your Matplotlib backend do not match. It does not normally mean the Python IPython package is missing from the kernel.

A common trigger is the legacy magic:

python
%matplotlib notebook
Output

On Jupyter Notebook 7, JupyterLab, VS Code notebooks, or Google Colab, switch to a supported backend instead:

python
%matplotlib widget
Output

On Jupyter Notebook 7, JupyterLab, or VS Code, install ipympl and use %matplotlib widget or %matplotlib ipympl. In Google Colab, also enable the custom widget manager. Use %matplotlib inline when interactive controls are unnecessary.

Tested on: Rocky Linux 10.2; Python 3.12.13; Jupyter Notebook 7.6.0; JupyterLab 4.6.1; Matplotlib 3.11.0; ipympl 0.10.0.


Which fix to use

Pick the row that matches your notebook environment or symptom. Each link jumps to the fix section for that case. If you are not sure which Jupyter front end you are running, start with Check your Jupyter environment and versions.

Your situation What to do Go to
Jupyter Notebook 7, JupyterLab, or VS Code; you want interactive plots Install ipympl, restart the kernel, run %matplotlib widget Fix 1: Use ipympl in Notebook 7 or JupyterLab
You only need a static plot image in the cell output Switch to %matplotlib inline Fix 2: Use %matplotlib inline for static plots
pip install ipympl worked in a terminal but the notebook still fails The kernel or Jupyter server may be using a different Python environment Fix 3: Resolve kernel and package environment mismatches
VS Code .ipynb notebook Confirm extensions, kernel, ipympl, and backend magic Fix 4: VS Code notebooks and Google Colab
Google Colab runtime Install ipympl and enable the custom widget manager Fix 4: VS Code notebooks and Google Colab
Notebook or extension calls IPython.notebook in JavaScript Update the extension or replace classic front-end APIs Fix 5: Old notebook extensions or JavaScript code
Old teaching notebook must keep %matplotlib notebook Treat nbclassic as a compatibility path Use nbclassic only for legacy notebooks
ModuleNotFoundError: No module named 'ipympl' Install ipympl into the active kernel, not just the terminal Fix 1: Use ipympl in Notebook 7 or JupyterLab
%matplotlib widget is set but the error or blank plot remains Work through the backend and environment checklist Troubleshoot when %matplotlib widget still fails

Choose the correct Matplotlib backend

Notebook environment Recommended backend Additional package
Jupyter Notebook 7 or newer %matplotlib widget or %matplotlib ipympl ipympl
JupyterLab %matplotlib widget or %matplotlib ipympl ipympl
VS Code notebook %matplotlib widget or %matplotlib ipympl ipympl
Google Colab %matplotlib widget with widget manager enabled ipympl
Static plots in any notebook %matplotlib inline None
Classic Notebook below version 7 %matplotlib notebook or %matplotlib nbagg None
nbclassic %matplotlib notebook can still work None

%matplotlib notebook turns on the older nbAgg backend. %matplotlib widget and %matplotlib ipympl activate the modern ipympl backend.

Official compatibility summary:

Backend Supported front ends
ipympl JupyterLab and Jupyter Notebook 7 or newer, VS Code notebooks, Google Colab
nbAgg Classic Notebook below version 7 and nbclassic

Why “IPython is not defined” appears

Three different ideas are easy to mix up:

Name What it is
Python IPython package Python library used by the notebook kernel
Notebook kernel Python process executing your cells
Browser IPython global JavaScript object exposed by classic Notebook front ends

Interactive Matplotlib modes and some extensions call JavaScript such as IPython.notebook.kernel. Jupyter Notebook 7 and JupyterLab do not expose that global the same way classic Notebook did. The browser then reports IPython is not defined even when Python cells run normally.

Installing IPython with pip does not fix that mismatch when the kernel already executes code successfully.

A common failing cell sequence looks like this:

python
%matplotlib notebook

import matplotlib.pyplot as plt

plt.plot([1, 2, 3])
plt.show()
Output

In Notebook 7 or JupyterLab, %matplotlib notebook targets the legacy nbAgg path. The front end no longer provides the JavaScript IPython object that backend expects, so the plot step fails in the browser.


Check your Jupyter environment and versions

A Jupyter deployment can use separate environments for the browser front end and Jupyter server, the Python kernel, a remote JupyterHub or hosted server, and the local VS Code application. Check the front-end version from the Jupyter interface or from the environment that launches the Jupyter server—not by inferring it from kernel-side packages.

From a terminal on the machine or environment that runs the Jupyter server, list the installed server and front-end packages:

bash
jupyter --version

Sample output:

output
Selected Jupyter core packages...
IPython          : 9.15.0
ipykernel        : 7.3.0
ipywidgets       : 8.1.8
jupyter_client   : 8.9.1
jupyter_core     : 5.9.1
jupyter_server   : 2.20.0
jupyterlab       : 4.6.1
nbclient         : 0.11.0
nbconvert        : 7.17.1
nbformat         : 5.10.4
notebook         : 7.6.0
traitlets        : 5.15.1

jupyter --version from the server environment identifies installed server and front-end packages. It does not prove which interface your current browser tab is using. Notebook 7 and nbclassic can coexist on the same Jupyter Server, so an installed notebook 7.x package does not by itself confirm that the open tab is the Notebook 7 front end. Confirm the active interface from the Jupyter UI—Help menu, About dialog, or the URL you opened (/lab, /tree, nbclassic, VS Code, or Colab).

Inside the affected notebook, check the active Python interpreter and Matplotlib backend:

python
import sys
import matplotlib

print("Python:", sys.executable)
print("Matplotlib:", matplotlib.__version__)
print("Backend:", matplotlib.get_backend())
Output

Sample output from a normal Jupyter notebook before you change backends:

output
Python: /usr/bin/python3
Matplotlib: 3.11.0
Backend: inline

Depending on Matplotlib and matplotlib-inline versions, the backend may instead appear as a module path such as module://matplotlib_inline.backend_inline.

A standalone Python process without a graphical display may report agg. Inside a normal Jupyter notebook, the initial backend is usually the static inline backend. After %matplotlib widget, the reported backend should identify the ipympl or widget backend.

Inspect kernel-side plotting packages with %pip in the notebook:

python
%pip show matplotlib ipympl ipywidgets
Output

Use that output to confirm:

  • ipympl is missing from the kernel → install it before using %matplotlib widget
  • the kernel’s Python path matches the environment where you expected packages to be installed
  • in VS Code, the selected kernel matches the environment shown by sys.executable in the notebook
  • in JupyterHub or other remote-kernel deployments, the server and kernel may use different environments—%pip show describes only the kernel side

Fix 1: Use ipympl in Notebook 7 or JupyterLab

Install ipympl in the active notebook kernel:

python
%pip install ipympl
Output

For Conda environments:

python
%conda install -c conda-forge ipympl
Output

When installing from a terminal instead, target the same interpreter the notebook kernel uses:

bash
python -m pip install ipympl

Restart the kernel after installation. Restart JupyterLab or Notebook if the front end still shows the old error.

Activate the backend before importing pyplot:

python
%matplotlib widget
Output

%matplotlib ipympl activates the same backend.

Test with a minimal plot:

python
%matplotlib widget

import matplotlib.pyplot as plt

fig, ax = plt.subplots()
ax.plot([1, 2, 3], [1, 4, 9])
ax.set_title("Interactive Matplotlib test")
plt.show()
Output

On current JupyterLab and Notebook 7 releases, installing ipympl in the kernel is usually enough when the server and kernel share one environment. You do not need legacy commands such as jupyter labextension install @jupyter-widgets/jupyterlab-manager unless you are on JupyterLab below version 3.

When the Jupyter server and notebook kernel use separate environments, installing ipympl only in the kernel may not be sufficient. ipympl includes both a Python backend that runs in the kernel and a JavaScript front-end component used by the notebook interface. In JupyterHub, remote-kernel, or other split deployments, confirm that the server environment provides a compatible jupyter-matplotlib front-end extension and that its version matches the kernel-side ipympl package. The official ipympl installation guide documents this front-end and back-end split and publishes a compatibility table.


Fix 2: Use %matplotlib inline for static plots

When pan, zoom, and toolbar controls are unnecessary, use the inline backend:

python
%matplotlib inline

import matplotlib.pyplot as plt

plt.plot([1, 2, 3], [1, 4, 9])
plt.show()
Output

This renders a static image in the cell output and avoids the interactive widget front end entirely. That is a valid choice when you only need the plot displayed, not a workaround for broken interactivity. For broader Matplotlib plotting patterns in Python, see the Python Matplotlib guide.


Fix 3: Resolve kernel and package environment mismatches

ipympl may install successfully into a different Conda environment, virtual environment, or system Python than the one your notebook kernel uses. The same problem appears when the Jupyter server environment and the kernel environment are different: a terminal install or jupyter --version check on the server host does not automatically update the kernel where %matplotlib widget runs.

Compare the kernel path from the notebook:

python
import sys
print(sys.executable)
Output

With the terminal Python on the same machine:

bash
python -c "import sys; print(sys.executable)"

When both lines print the same path, the kernel and terminal share one interpreter.

Verify that ipympl imports inside the notebook:

python
import ipympl
print(ipympl.__version__)
Output

Sample output after a successful install:

output
0.10.0

Recommended recovery steps:

  1. Select the correct notebook kernel in the UI.
  2. Install ipympl with %pip or %conda from that kernel.
  3. Restart the kernel.
  4. Reload the notebook front end.
  5. Run the backend magic before creating any figures.

Repeatedly running pip install IPython does not fix an environment mismatch when Python cells already execute and only the browser reports IPython is not defined.


Fix 4: VS Code notebooks and Google Colab

VS Code notebooks

Confirm that:

  • the Microsoft Python and Jupyter extensions are enabled
  • the correct Python kernel or environment is selected from the notebook kernel selector for the .ipynb file
  • ipympl is installed in that kernel
  • the notebook uses %matplotlib widget, not %matplotlib notebook
  • VS Code is reloaded after installing or updating notebook packages

Run the same minimal interactive test plot from Fix 1 after changing the backend.

Google Colab

Install the backend in the runtime:

python
%pip install ipympl
Output

Enable Colab’s custom widget manager:

python
from google.colab import output
output.enable_custom_widget_manager()
Output

Then activate the widget backend:

python
%matplotlib widget
Output

Restart the Colab runtime if widgets still fail to render after installation.


Fix 5: Old notebook extensions or JavaScript code

Some notebooks or extensions still contain JavaScript like:

javascript
IPython.notebook.kernel.execute("print('Hello')");
Output

That API belongs to classic Notebook JavaScript and is not portable to JupyterLab or Notebook 7.

Recommended actions:

  1. Update the package or notebook extension.
  2. Check for a Notebook 7 or JupyterLab-compatible release.
  3. Replace direct front-end access with supported widgets, comms, or a JupyterLab extension.
  4. Contact the package maintainer when the JavaScript is generated by a third-party Python library.

Do not define a fake global IPython object in the browser to silence the error.

Legacy JupyterLab 1 and 2 extension commands

JupyterLab versions below 3 required building the widget extensions manually:

bash
jupyter labextension install @jupyter-widgets/jupyterlab-manager
jupyter labextension install jupyter-matplotlib

JupyterLab 3 and 4 normally use the prebuilt extension installed with ipympl; do not run these legacy Node.js build commands in the modern default workflow. The official ipympl installation guide places these commands under JupyterLab below version 3.


Use nbclassic only for legacy notebooks

nbclassic may be appropriate when:

  • an old teaching notebook depends on %matplotlib notebook
  • a critical extension has no Notebook 7-compatible version
  • custom JavaScript depends on classic Notebook APIs

Treat it as a compatibility path, not the primary modern solution.

Downgrading to notebook<7 can reproduce classic behaviour, but migrating to ipympl or a supported extension is the better long-term fix.


Troubleshoot when %matplotlib widget still fails

Symptom Likely cause Action
Step Check
1 Confirm import ipympl succeeds in the active kernel
2 Restart the kernel after installation
3 Restart JupyterLab, Notebook, VS Code, or the Colab runtime
4 Run the backend magic before the first figure
5 Do not switch between notebook, inline, and widget repeatedly in one kernel
6 Inspect matplotlib.get_backend()
7 Confirm compatible Matplotlib, ipympl, JupyterLab, and widget versions—the tested combination here (ipympl 0.10.0, JupyterLab 4.6.1, Matplotlib 3.11.0) is within the current official compatibility range
8 Test a clean notebook with only the minimal plot
9 Disable outdated notebook extensions when the clean notebook works
10 Review browser-console errors only after backend and environment checks
output
ModuleNotFoundError: No module named 'ipympl'

That means the Python package is missing from the current kernel. Install ipympl with %pip install ipympl in the notebook, restart the kernel, and run %matplotlib widget again. It is a different failure mode from the browser-side IPython is not defined message.


Common mistakes to avoid

  • Installing IPython without checking the Matplotlib backend
  • Copying JupyterLab 1 or 2 extension-build commands into JupyterLab 3 or 4
  • Installing packages in the terminal without checking which kernel—or Jupyter server environment—the notebook uses
  • Continuing to use %matplotlib notebook in Notebook 7
  • Downgrading the complete Jupyter environment before trying ipympl
  • Mixing multiple Matplotlib backends in one kernel session
  • Assuming every IPython is not defined error comes from Matplotlib; some originate in old third-party JavaScript

Summary

  • Use %matplotlib inline for static output.
  • Use ipympl with %matplotlib widget or %matplotlib ipympl for modern interactive notebooks.
  • Use %matplotlib notebook only with classic Notebook below version 7 or nbclassic.
  • Check the active kernel and, in split deployments, the server-side jupyter-matplotlib extension when installation appears successful but the error continues.
  • Update old extensions that access IPython.notebook directly.

References


Frequently Asked Questions

1. Does installing the Python IPython package fix this error?

Usually not. The message comes from notebook frontend JavaScript expecting a browser global named IPython. When Python cells otherwise run, the fix is normally the Matplotlib backend or an outdated notebook extension, not pip install IPython.

2. What should I use instead of %matplotlib notebook in Jupyter Notebook 7?

Install ipympl in the active kernel, restart the kernel, then run %matplotlib widget or %matplotlib ipympl before creating figures. Use %matplotlib inline when you only need static plots.

3. Are %matplotlib widget and %matplotlib ipympl the same?

Yes. Both activate the ipympl backend for interactive Matplotlib figures in modern Jupyter front ends.

4. Why does ipympl import work in the terminal but not in the notebook?

The notebook kernel may use a different Python interpreter than the terminal where you ran pip install, or the Jupyter server and kernel may run in separate environments. Install with %pip or %conda from the notebook, confirm sys.executable, and compare kernel-side ipympl with the server-side jupyter-matplotlib extension when widgets stay blank.

5. Is ModuleNotFoundError for ipympl the same as IPython is not defined?

No. ModuleNotFoundError means the Python package is missing from the active kernel. IPython is not defined is a browser-side JavaScript error, often from the legacy nbAgg backend on an unsupported front end.
Olorunfemi Akinlua

Boasting over five years of experience in JavaScript, specializing in technical content writing and UX design. With a keen focus on programming languages, he crafts compelling content and designs user-friendly interfaces to enhance digital experiences across various domains.

  • JavaScript
  • Web Design