TUDelft-CITG / learn-python

Python for Engineers
https://tudelft-citg.github.io/learn-python/
2 stars 3 forks source link

matplotlib inline does not work #35

Open rlanzafame opened 1 year ago

rlanzafame commented 1 year ago

The IPython console is not working with matplotlib. It's not a generic import, since import numpy as np works. Below the error report from 2 attempts at using it:

import matplotlib as plt

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/xpython_42/2917018121.py in <cell line: 1>()
----> 1 import matplotlib as plt

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/matplotlib/__init__.py in <module>
    206 
    207 
--> 208 _check_versions()
    209 
    210 

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/matplotlib/__init__.py in _check_versions()
    201     ]:
    202         module = importlib.import_module(modname)
--> 203         if parse_version(module.__version__) < parse_version(minver):
    204             raise ImportError(f"Matplotlib requires {modname}>={minver}; "
    205                               f"you have {module.__version__}")

AttributeError: module 'kiwisolver' has no attribute '__version__'
%matplotlib inline

Error:

---------------------------------------------------------------------------
AttributeError                            Traceback (most recent call last)
/tmp/xpython_42/352233182.py in <cell line: 1>()
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/IPython/core/interactiveshell.py in run_line_magic(self, magic_name, line, _stack_depth)
   2405                 kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2406             with self.builtin_trap:
-> 2407                 result = fn(*args, **kwargs)
   2408             return result
   2409 

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/decorator.py in fun(*args, **kw)
    230             if not kwsyntax:
    231                 args, kw = fix(args, kw, sig)
--> 232             return caller(func, *(extras + args), **kw)
    233     fun.__name__ = func.__name__
    234     fun.__doc__ = func.__doc__

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/IPython/core/magic.py in <lambda>(f, *a, **k)
    185     # but it's overkill for just that one bit of state.
    186     def magic_deco(arg):
--> 187         call = lambda f, *a, **k: f(*a, **k)
    188 
    189         if callable(arg):

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/IPython/core/magics/pylab.py in matplotlib(self, line)
     97             print("Available matplotlib backends: %s" % backends_list)
     98         else:
---> 99             gui, backend = self.shell.enable_matplotlib(args.gui.lower() if isinstance(args.gui, str) else args.gui)
    100             self._show_matplotlib_backend(args.gui, backend)
    101 

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/IPython/core/interactiveshell.py in enable_matplotlib(self, gui)
   3598         """
   3599         from IPython.core import pylabtools as pt
-> 3600         from matplotlib_inline.backend_inline import configure_inline_support
   3601         gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
   3602 

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/matplotlib_inline/__init__.py in <module>
----> 1 from . import backend_inline, config  # noqa
      2 __version__ = "0.1.6"  # noqa

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/matplotlib_inline/backend_inline.py in <module>
      4 # Distributed under the terms of the BSD 3-Clause License.
      5 
----> 6 import matplotlib
      7 from matplotlib import colors
      8 from matplotlib.backends import backend_agg

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/matplotlib/__init__.py in <module>
    206 
    207 
--> 208 _check_versions()
    209 
    210 

/tmp/xeus-python-kernel/envs/xeus-python-kernel/lib/python3.10/site-packages/matplotlib/__init__.py in _check_versions()
    201     ]:
    202         module = importlib.import_module(modname)
--> 203         if parse_version(module.__version__) < parse_version(minver):
    204             raise ImportError(f"Matplotlib requires {modname}>={minver}; "
    205                               f"you have {module.__version__}")

AttributeError: module 'kiwisolver' has no attribute '__version__'
rlanzafame commented 1 year ago

it seems like it could be because ipympl is not included. first added to *.yml in JSON; didn't work, but i only re-ran an old job. then tried with a new commit to ./book/ to trigger a complete new build...

rlanzafame commented 1 year ago

@CasparJungbacker any ideas here? Does the same thing happen to you when you try using matplotlib: a) in the browser on your mac, and/or b) using local build on mac?

rlanzafame commented 1 year ago

I can't remember if this is a known issue with no solution. If so, the workaround will be to set up a classic nb using pyodide at github.com/TUDelft-CITG/learn-python-nb to be able to do the mpl stuff in the mooc

CasparJungbacker commented 1 year ago

@CasparJungbacker any ideas here? Does the same thing happen to you when you try using matplotlib: a) in the browser on your mac, and/or b) using local build on mac?

I've had this error before, but it was because matplotlib was not included in the build process of the kernel which is not the case now. On my laptop, I use Anaconda for building the kernel, though, while the pipeline uses Mamba, so there might be something fishy going on there. I'll try if switching back to Anaconda fixes it.

rlanzafame commented 1 year ago

Hmm, ok, sounds optimistic. i was looking at this repo for reference, and they have ipympl but as you can see here they also seem to use mamba

rlanzafame commented 1 year ago

We aren't going to use JupyterLite in the book build this summer, so this Issue becomes irrelevant. See Archive pre-release and deploy.yml, which should replicate this issue. We should work this out, but let's do it in the interactivetextbooks-citg Group.