[Bug]: No module named 'ipywidgets'
PyBaMM Version
24.1
Python Version
3.11.9
Describe the bug
I am running tutorial 1 in jupyter lab (Version 4.1.6) at Miniforge ( https://github.com/conda-forge/miniforge/releases/latest/download/Miniforge3-Windows-x86_64.exe ) and error occurs when I execute sim.plot() .
Steps to Reproduce
Simply run the tutorial
Relevant log output
---------------------------------------------------------------------------
ModuleNotFoundError Traceback (most recent call last)
Cell In[5], line 1
----> 1 sim.plot()
File ~\AppData\Local\miniforge-pypy3\envs\pybamm\Lib\site-packages\pybamm\simulation.py:1054, in Simulation.plot(self, output_variables, **kwargs)
1051 if output_variables is None:
1052 output_variables = self._output_variables
-> 1054 self.quick_plot = pybamm.dynamic_plot(
1055 self._solution, output_variables=output_variables, **kwargs
1056 )
1058 return self.quick_plot
File ~\AppData\Local\miniforge-pypy3\envs\pybamm\Lib\site-packages\pybamm\plotting\dynamic_plot.py:21, in dynamic_plot(*args, **kwargs)
19 kwargs_for_class = {k: v for k, v in kwargs.items() if k != "testing"}
20 plot = pybamm.QuickPlot(*args, **kwargs_for_class)
---> 21 plot.dynamic_plot(kwargs.get("testing", False))
22 return plot
File ~\AppData\Local\miniforge-pypy3\envs\pybamm\Lib\site-packages\pybamm\plotting\quick_plot.py:666, in QuickPlot.dynamic_plot(self, testing, step)
653 """
654 Generate a dynamic plot with a slider to control the time.
655
(...)
663
664 """
665 if pybamm.is_notebook(): # pragma: no cover
--> 666 import ipywidgets as widgets
668 step = step or self.max_t / 100
669 widgets.interact(
670 lambda t: self.plot(t, dynamic=False),
671 t=widgets.FloatSlider(
(...)
674 continuous_update=False,
675 )
ModuleNotFoundError: No module named 'ipywidgets'
So in the first cell the install of pybamm with the plot dependencies is commented out. Probably just need to make sure those are installed. If that does not work then I will look into this further
I had to comment out due to another error which does not seem to change the result. Here is a screenshot after removing the comment. The same error appears when execute sim.plot()
It looks like the install failed with an error due to the comment after the -q flag, can you remove the comment in the first line and see if it installs correctly?
Removing the comment after the -q flag solves the error at the line but still sim.plot() generates the same error
Ok I will take a look then. Thanks for checking
We can consider declaring ipywidgets as an optional dependency under [plot].
We can consider declaring
ipywidgetsas an optional dependency under[plot].
Yeah still good to know why there is a difference with this setup
Yeah still good to know why there is a difference with this setup
I found the problem – pybamm.QuickPlot.dynamic_plot requires it, but the import is hidden inside the is_notebook() function, and therefore PyBaMM does not fail on import. I think adding it to the [plot] dependencies and replacing the import with import_optional_dependency() should be alright (along with corresponding changes in the installation guide).
@agriyakhetarpal, @swsyoon So I checked on both pip and anaconda packages, ipywidgets is a dependency of Jupyter. This sounds like it might be an issue with the miniforge version of Jupyter lab rather than an issue with PyBaMM itself
Thanks for checking – Miniforge is just the same as conda/mamba but tailored for the conda-forge channel rather than defaults. So, yes, the notebook should work without changes from our end.