Imagine you are deep in your workflow with Jupyter Notebook or JupyterLab, you run a cell expecting an interactive plot or a nifty widget and instead you’re met with the dreaded error: “JavaScript Error: IPython is not defined”. Frustrating, right? You expected your plot to show up. Instead you’re faced with confusion.
What Exactly Does “IPython Is Not Defined”
When you see JavaScript Error: IPython is not defined, what’s happening under the hood, The JavaScript environment (browser front-end) is trying to access a global object called IPython. That object was used in older versions of Jupyter Notebook to interface between JavaScript and the Python kernel. In newer versions (especially Notebook 7+ and JupyterLab), the architecture has changed: the legacy IPython.notebook object may no longer be available or supported in the same way.
If your code (or a library you’re using) expects IPython to exist (for example to call IPython.notebook.kernel.execute(...)), and it doesn’t, then the browser throws that error.
So in plain-English: the front-end JavaScript is looking for a variable that simply doesn’t exist in the current environment. Either because you’re not in a classic notebook front-end, or because you’re using a backend (interactive plot tool) that relies on older interfaces.
Common Scenarios Where You’ll See It
Here are the most frequent situations that trigger this error:
Using %matplotlib notebook or %matplotlib inline:
Many tutorials use %matplotlib notebook to get interactive plots. But with JupyterLab (or Notebook version 7+) this may not work properly. In fact, multiple sources point to using %matplotlib widget (via ipympl) instead.
You’re using JupyterLab or Notebook 7+ but your code/libraries assume the old IPython object
In newer front-ends the classic IPython global may not be exposed. Some libraries still rely on it. Example posts describe this exact mismatch.
Mixed or outdated installations:
You might have multiple versions of Jupyter, ipython, matplotlib, or ipympl installed. Conflicts may hide or disable the older interfaces. For example, installing ipympl is suggested repeatedly as a fix.
Running JavaScript in a non-notebook environment but assuming IPython is defined:
If you’re writing JS that expects to run inside a notebook cell (with access to the kernel via IPython), but instead you’re in a plain HTML page or other context, you’ll hit this error too. One post describes mixing JS and Python incorrectly.
How to Fix the Error
Let’s fix it. I’ll walk through a progressive approach: quick fix, interactive fix, deep fix, and prevention.
Quick fix for non-interactive plots:
If you don’t necessarily need interactive plots, you can avoid the feature entirely. In a notebook cell (before your plotting code), do:
%matplotlib inline
This tells matplotlib to render plots as static images, not interactive widgets. Many posts show this works.
It’s a simple workaround. The downside: you lose interactivity (zooming, panning, widgets).
Interactive fix using ipympl:
- If you do want interactive plots, use
ipympl. Here’s how: - Install it (choose one depending on your environment):
pip install ipympl
# or
conda install -c conda-forge ipympl
- Multiple sources recommend this.
- Restart your notebook / kernel / browser session to flush caches. One blog emphasises restarting everything after install.
- In the notebook cell before plotting:
%matplotlib widget
That magic sets the backend to widget which uses ipympl and avoids reliance on IPython global.
Run your plotting code again. The JS error should go away, interactive plots should show up.
Deep fix align your environment:
If the above still fails, check the following:
- Confirm your versions of Jupyter Notebook or JupyterLab. If you’re using Notebook 7+ (which is built on JupyterLab components) then ‘
%matplotlib notebook‘ might not be supported. - Ensure that you only have one or consistent installation of the kernel and front-end. Multiple kernels or mismatched front-end/back-end combos might cause the
IPythonobject to be missing. - If you’re using extensions (like
jupyter-widgets,jupyter-matplotlib, etc), make sure they are installed and enabled. For example, in JupyterLab 2 you might need:
conda install nodejs
jupyter labextension install @jupyter-widgets/jupyterlab-manager
jupyter labextension install jupyter-matplotlib
jupyter lab build
Prevention set yourself up right:
To avoid this error down the line:
- Always use
%matplotlib widget(with ipympl) if you want interactivity in JupyterLab or Notebook 7+. - Avoid
%matplotlib notebookunless you’re sure it’s supported in your version. - Keep your packages (jupyter, notebook, ipython, matplotlib, ipympl, widgets) updated.
- Test the simple statement:
console.log(typeof IPython);
- In a browser console open on your notebook page. If it returns
undefined, you know ahead of time theIPythonglobal isn’t available. - If you distribute notebooks or collaborate, make a short note or cell explaining which magic command to use (inline or widget) and what version environment you expect.
- Use environment-specific instructions so collaborators don’t run into hidden version mismatches.
Why This Error Is Becoming More Common:
You might wonder: “Why didn’t I see this error before?” A large reason is that Jupyter’s architecture has evolved.
- The classic IPython global object was more central in older versions of Jupyter Notebook (pre-version 7) and in classic notebook front-ends.
- With Notebook 7+, the front-end is built on JupyterLab components, and some legacy JS globals have been removed or disabled.
- Many third-party extensions or tutorials were built for the old architecture, and haven’t been updated accordingly.
- Interactive plotting (matplotlib backend) also changed:
%matplotlib notebookwas the default interactive command, but now%matplotlib widgetwith ipympl is the more future-proof choice.
Conclusion
The “JavaScript Error: IPython is not defined” looks intimidating, but it’s really just a sign that your notebook’s front end and backend are speaking different languages. The old %matplotlib notebook command relies on a JavaScript hook that doesn’t exist in the newer Jupyter architecture. The modern fix is simple: use %matplotlib widget with the ipympl library for interactive plots, or %matplotlib inline if you’re fine with static images. Keep your environment updated, restart your kernel when things act weird, and avoid legacy commands that depend on IPython.notebook.

