Plotly ImportError and ModuleNotFoundError in Jupyter: Causes and Fixes

Match your Plotly import error to its fix. No module named plotly means the notebook kernel is a different Python, Plotly Express requires numpy or pandas means a missing optional package, and the plotly.plotly and anywidget errors have one-line answers.

Most Plotly import errors come down to one of two things: the package isn't installed in the Python that your notebook kernel runs, or an optional dependency is missing. Plotly 7.2 installs only narwhals and packaging as dependencies, so run pip install "plotly[express]" pandas nbformat and restart the kernel. The error text and behavior below come from Plotly 7.2.0, with Plotly 5.24 and 6.x checked where they differ.

Quick Example

Install everything a typical notebook chart needs in one go. In a notebook cell, use %pip so the packages go into the running kernel's environment:

%pip install -U "plotly[express]" pandas nbformat

Restart the kernel, then this should draw a chart:

import numpy as np
import pandas as pd
import plotly.express as px

rng = np.random.default_rng(21)
weeks = np.arange(1, 13)
df = pd.DataFrame({
    "week": np.tile(weeks, 2),
    "channel": np.repeat(["Web", "App"], len(weeks)),
    "signups": np.concatenate([
        (200 + 12 * weeks + rng.normal(0, 10, len(weeks))).round(),
        (90 + 25 * weeks + rng.normal(0, 12, len(weeks))).round(),
    ]),
})

fig = px.line(df, x="week", y="signups", color="channel", markers=True,
              color_discrete_sequence=["#264653", "#e76f51"], title="Weekly signups by channel")
fig.show()

Line chart of weekly signups over 12 weeks for web and app channels, with the app line starting lower and climbing past the web line in week 8

If that works, you're done. If it raises an error, find your message below.

Which Plotly import error do you have?

Error text Cause Fix
ModuleNotFoundError: No module named 'plotly' Plotly isn't in the kernel's Python %pip install plotly, then restart
ImportError: Plotly Express requires numpy to be installed Plotly doesn't install numpy pip install "plotly[express]"
ImportError: Plotly express requires pandas to be installed Plotly 5 needs pandas pip install pandas
NotImplementedError: Pandas installation is required if no dataframe is provided px got lists, arrays, or a dict and has no pandas pip install pandas
ValueError: Mime type rendering requires nbformat>=4.2.0 The notebook renderer needs nbformat pip install nbformat
ValueError: Mime type rendering requires ipython No IPython in this environment pip install ipython (Jupyter already includes it)
ImportError: Please install anywidget to use the FigureWidget class go.FigureWidget needs anywidget pip install anywidget
ModuleNotFoundError: No module named 'plotly.plotly' Code written for Plotly 3 or older Use fig.show(), or pip install chart-studio

ModuleNotFoundError: No module named 'plotly'

You installed Plotly, but the notebook runs a different Python. This happens when pip belongs to one environment and the kernel uses another. Check which interpreter the kernel is using and whether it can see the packages:

import importlib.util
import sys

print(sys.executable)
for pkg in ["plotly", "numpy", "pandas", "narwhals", "IPython", "nbformat", "anywidget"]:
    print(f"{pkg:10}", "installed" if importlib.util.find_spec(pkg) else "MISSING")

The printed path is the Python that has to contain Plotly. Install into exactly that interpreter, from a cell inside the notebook:

%pip install plotly

%pip targets the running kernel, which a !pip install in a cell or a pip install in a terminal may not. Restart the kernel afterward, because a package installed after the notebook started isn't importable until the kernel restarts. In VS Code, also check the kernel picker in the top right: it should name the environment where you installed Plotly. The same mismatch causes the error for every package, as in Plotly not showing in Jupyter or VS Code.

ImportError: Plotly Express Requires numpy (or pandas)

import plotly.express as px fails with this on Plotly 6 and 7 when numpy is missing:

ImportError: Plotly Express requires numpy to be installed. You can install numpy using pip with:

$ pip install numpy

Plotly doesn't install numpy or pandas for you, so a fresh environment has neither. The express extra pulls in what import plotly.express needs:

pip install "plotly[express]"

Plotly 5.x has the same problem with a different message, ImportError: Plotly express requires pandas to be installed, and the fix there is pip install pandas. Plain plotly.graph_objects charts need neither package.

NotImplementedError: Pandas Installation Is Required

On Plotly 6 and 7, plotly[express] brings numpy but not pandas. Everything works until you pass Plotly Express something that isn't a dataframe:

NotImplementedError: Pandas installation is required if no dataframe is provided.

px.scatter(x=[1, 2], y=[3, 4]), a NumPy array, and a plain dict all hit this, because Plotly Express turns them into a pandas dataframe first. Run pip install pandas and restart the kernel.

ValueError: Mime Type Rendering Requires nbformat or ipython

These come from fig.show(), not from the import, and they're covered in full in Plotly not showing in Jupyter or VS Code. The short version: the notebook renderers need nbformat, and in an environment without Jupyter they also need ipython.

ValueError: Mime type rendering requires nbformat>=4.2.0 but it is not installed
ValueError: Mime type rendering requires ipython but it is not installed

Install nbformat (and ipython if the second message appears) into the kernel's environment and restart. The ipython message is the one you see first in a bare environment, and the nbformat one appears after it's fixed.

ImportError: Please Install anywidget to Use FigureWidget

go.FigureWidget is the interactive notebook widget version of a figure. It's the only part of Plotly that needs anywidget:

ImportError: Please install anywidget to use the FigureWidget class

Run pip install anywidget and restart the kernel. Regular go.Figure and fig.show() never need it.

ModuleNotFoundError: No Module Named 'plotly.plotly'

Old tutorials start with import plotly.plotly as py. That module was the connection to Plotly's online service, and it was removed in Plotly 4. The error is ModuleNotFoundError: No module named 'plotly.plotly', and the fix is to switch to the current calls:

Old code Now
py.iplot(fig) or py.plot(fig) fig.show()
py.plot(fig, filename="x.html") fig.write_html("x.html")
py.image.save_as(fig, "x.png") fig.write_image("x.png") (needs Kaleido)
Hosting charts on Chart Studio pip install chart-studio, then import chart_studio.plotly as py

from plotly.offline import iplot, init_notebook_mode still imports on Plotly 7, so code that uses the offline functions keeps working. The usual remaining difference is import plotly.graph_objs as go, which also still works, though the current name is plotly.graph_objects.

Practical Tips

  • Install into the kernel's Python. Use %pip in a notebook, or python -m pip install ... with the same interpreter that starts Jupyter, so the two can't disagree.
  • Restart the kernel after installing. The error persists in a running kernel even after a successful install.
  • List the optional packages in your requirements. Put plotly[express], pandas, and nbformat in requirements.txt or pyproject.toml, since pip install plotly won't add them.
  • Match the error to the version. pip show plotly or plotly.__version__ tells you whether the 5.x or 6+ message applies.
  • Static image errors are a separate problem. write_image needs Kaleido and Chrome, not these packages.

Similar Topics