No module named 'ipympl' found

I am using jupyter lab for 3D plotting with interactive charts. The strange thing is that the same code works perfectly on one computer and doesnt in another yet they have the same modules installed. The ipympl is installed and it appears on pip list command. I looked at some posts but nothing appears to help. Any help i greatly appreciated.
.
Here is the first few lines

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import ipympl
%matplotlib widget

ipympl can be a but tricky to install well and get all the changes propagated as there’s many layers it affects. Make sure to do a hard refresh in the browser window on the page you are trying to use to run code employing it.
Have you tried multiple browsers on the computer that it doesn’t work on?

You aren’t clear at all though on what happens when you try to run code using it? And that code provided is incomplete. I wouldn’t expect anything to happen with it. Please share what happens with a minimal reproducible example you can share. There should be examples among code here and Stackoverflow. Or go here press ‘launch binder’ and when the session starts scroll down about a third of the way. There’s examples using it there, too.

Oh wait, is the title what happens when you try to use it? It would be best to be clear about all this.
If that is the case then I’d ask how are you running pip list. Are you using the magic %pip list in a cell in the notebook? That is the one that will tell you what is actually present in the environment backing the kernel.

And if it is still being a problem, you may want to try & go ahead and try adding a new cell above the one where you want to use ipympl and then run the magic %pip install ipympl. Then refresh the kernel but don’t try running the code yet. Shutdown everything on the machine. The browser, the entire machine and restart it. Then when you open the notebook again, do a hard page refresh on the notebook page in the browser. JupyterLab should refresh the entire page. Give it time. Then restart the kernel and only now try code that uses ipympl.


Tip: Although %matplotlib widget is supported to allow legacy code to work, it is best to use %matplotlib ipympl as that is more explicit and actually what is being used. That way you and others will know ipympl is involved.

Thanks so much for a detailed response. I tried a different browser w/o success. I used %pip list which doesn’t show ipympl as an installed module. In contrast to my other computer where I everything works, ipympl was listed using %pip list. I read a little about it and it may be an environment variable issue.

Here is my code and what happens when I run the code

import ipympl

%matplotlib widget

from mpl_toolkits.mplot3d import Axes3D

import matplotlib.pyplot as plt

creating random dataset

xs = [14, 24, 43, 47, 54, 66, 74, 89, 12, 44, 1, 2, 3, 4, 5, 9, 8, 7, 6, 5]

ys = [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 6, 3, 5, 2, 4, 1, 8, 7, 0, 5]

zs = [9, 6, 3, 5, 2, 4, 1, 8, 7, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0]

creating figure

fig = plt.figure()

ax = Axes3D(fig,auto_add_to_figure=False)

fig.add_axes(ax)

creating the plot

plot_3D = ax.scatter(xs, ys, zs, color=‘green’)

setting title and labels

ax.set_title(“3D plot”)

ax.set_xlabel(‘x-axis’)

ax.set_ylabel(‘y-axis’)

ax.set_zlabel(‘z-axis’)

displaying the plot

plt.show()

---------------------------------------------------------------------------

ModuleNotFoundError Traceback (most recent call last)

Cell In[1], line 1

----> 1 import ipympl

2 get_ipython().run_line_magic(‘matplotlib’, ‘widget’)

3 from mpl_toolkits.mplot3d import Axes3D

ModuleNotFoundError: No module named ‘ipympl’

that suggests it is not installed in the same environment the notebook is running in. This might mean you want to install it in the notebook environment:

You can install it from within the notebook with %pip install ipympl, or make sure you install it with the right python by checking the value of sys.executable in the notebook and using that in /path/to/the/right/bin/python3 -m pip install ipympl.

or it might mean you want to launch the notebook in the environment where you already have ipympl.

1 Like

Yes, and pay close attention to how I spelled out what to do after that when I suggested using the magic variation of the pip install command above in my original reply. ipympl changes a lot of things that don’t easily get changed without going through extra hoops, the sort of things you don’t usually need when installing other packages, like say pandas. That hard refresh in the browser on the notebook page is important with ipympl.

For some context about magic variation of the %pip list:
The magic pip command variation was added in 2019 to ensure the the pip commands like list and install act in the environment where the kernel is running that backs the active notebook.
See more about the modern %pip command here.

1 Like

Thanks so much. The %pip install works perfectly.
Thanks again

1 Like