Skip to content

AttributeError: module 'matplotlib' has no attribute '__version_info__' #60

@scheemik

Description

@scheemik

I ran into the following error when importing matplotlib (using version 3.4.3) in a Jupyter Notebook:

AttributeError: module 'matplotlib' has no attribute '__version_info__'

TLDR

I was able to work around this issue by specifying matplotlib-inline to be "<0.2.0" (essentially fixing it to "0.1.7").

It seems like this is a specific version incompatibility between matplotlib and matplotlib-inline which is only an issue when using maptlotlib "<3.5". However, I wanted to document what I found in case anyone else runs into similar issues.

Steps to reproduce

Create an environment with Python 3.9, matplotlib version 3.4.3, and matplotlib-inline version "<=0.2.0" then use that environment to run the following code in a Jupyter Notebook:

import matplotlib.pyplot as plt

plt.show()

This results in an error. Here is the full output:

AttributeError                            Traceback (most recent call last)
Cell In[2], line 1
----> 1 import matplotlib.pyplot as plt
      3 plt.show()

File ~/miniconda3/envs/to_delete0/lib/python3.9/site-packages/matplotlib/pyplot.py:2500
   2498     dict.__setitem__(rcParams, \"backend\", rcsetup._auto_backend_sentinel)
   2499 # Set up the backend.
-> 2500 switch_backend(rcParams[\"backend\"])
   2502 # Just to be safe.  Interactive mode can be turned on without
   2503 # calling `plt.ion()` so register it again here.
   2504 # This is safe because multiple calls to `install_repl_displayhook`
   2505 # are no-ops and the registered function respect `mpl.is_interactive()`
   2506 # to determine if they should trigger a draw.
   2507 install_repl_displayhook()

File ~/miniconda3/envs/to_delete0/lib/python3.9/site-packages/matplotlib/pyplot.py:277, in switch_backend(newbackend)
    270 # Backends are implemented as modules, but \"inherit\" default method
    271 # implementations from backend_bases._Backend.  This is achieved by
    272 # creating a \"class\" that inherits from backend_bases._Backend and whose
    273 # body is filled with the module's globals.
    275 backend_name = cbook._backend_module_name(newbackend)
--> 277 class backend_mod(matplotlib.backend_bases._Backend):
    278     locals().update(vars(importlib.import_module(backend_name)))
    280 required_framework = _get_required_interactive_framework(backend_mod)

File ~/miniconda3/envs/to_delete0/lib/python3.9/site-packages/matplotlib/pyplot.py:278, in switch_backend.<locals>.backend_mod()
    277 class backend_mod(matplotlib.backend_bases._Backend):
--> 278     locals().update(vars(importlib.import_module(backend_name)))

File ~/miniconda3/envs/to_delete0/lib/python3.9/importlib/__init__.py:127, in import_module(name, package)
    125             break
    126         level += 1
--> 127 return _bootstrap._gcd_import(name[level:], package, level)

File ~/miniconda3/envs/to_delete0/lib/python3.9/site-packages/matplotlib_inline/__init__.py:1
----> 1 from . import backend_inline, config  # noqa
      3 __version__ = \"0.2.1\"
      5 # we can't ''.join(...) otherwise finding the version number at build time requires
      6 # import which introduces IPython and matplotlib at build time, and thus circular
      7 # dependencies.

File ~/miniconda3/envs/to_delete0/lib/python3.9/site-packages/matplotlib_inline/backend_inline.py:236
    231                 ip.events.unregister(\"post_run_cell\", configure_once)
    233             ip.events.register(\"post_run_cell\", configure_once)
--> 236 _enable_matplotlib_integration()
    239 def _fetch_figure_metadata(fig):
    240     \"\"\"Get some metadata to help with displaying a figure.\"\"\"

File ~/miniconda3/envs/to_delete0/lib/python3.9/site-packages/matplotlib_inline/backend_inline.py:215, in _enable_matplotlib_integration()
    211 ip = get_ipython()
    213 import matplotlib
--> 215 if matplotlib.__version_info__ >= (3, 10):
    216     backend = matplotlib.get_backend(auto_select=False)
    217 else:

AttributeError: module 'matplotlib' has no attribute '__version_info__'"

The output indicates that the issue is in the backend_inline module of matplotlib_inline. In that file, it calls for matplotlib.__version_info__. However, in matplotlib version 3.4.3, that variable doesn't exist. The variable __version_info__ seems to be introduced in the next release, matplotlib version 3.5.

From testing environments with different versions of matplotlib_inline, I found that this error is due to the introduction of the call to matplotlib.__version_info__ in version 0.2.0 of matplotlib_inline. I took a look at the .toml file for this project, but didn't make a pull request because I'm unfamiliar with how it is handling version dependencies. Is there a way to specify that matplotlib_inline version ">=0.2.0" is incompatible with matplotlib version "<=3.4.3"?

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions