Search code examples
matplotlibmodulemodulenotfounderror

No module named 'matplotlib.backends.registry'


I started learning matplotlib with the Python Data Science Handbook by Jake VanderPlas that starts with this code for importing and setting style:

%matplotlib inline
import matplotlib.pyplot as plt
plt.style.use('seaborn-whitegrid')
import numpy as np

First, I got this error:

'seaborn-whitegrid' is not a valid package style, path of style file, URL of style file, or library style name (library styles are listed in style.available)

I tried:

print(plt.style.available)

But the 'seaborn-whitegrid' was not listed. Then I went for updating packages:

pip install --upgrade matplotlib seaborn

Still it did not work. Now I get this error:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
      2 import matplotlib.pyplot as plt
      3 plt.style.use('seaborn-whitegrid')

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\IPython\core\interactiveshell.py:2480, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2478     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2479 with self.builtin_trap:
-> 2480     result = fn(*args, **kwargs)
   2482 # The code below prevents the output from being displayed
   2483 # when using magics with decorator @output_can_be_silenced
   2484 # when the last Python token in the expression is a ';'.
   2485 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\IPython\core\magics\pylab.py:103, in PylabMagics.matplotlib(self, line)
     98     print(
     99         "Available matplotlib backends: %s"
    100         % _list_matplotlib_backends_and_gui_loops()
    101     )
    102 else:
--> 103     gui, backend = self.shell.enable_matplotlib(args.gui)
    104     self._show_matplotlib_backend(args.gui, backend)

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\IPython\core\interactiveshell.py:3665, in InteractiveShell.enable_matplotlib(self, gui)
   3662     import matplotlib_inline.backend_inline
   3664 from IPython.core import pylabtools as pt
-> 3665 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
   3667 if gui != None:
   3668     # If we have our first gui selection, store it
   3669     if self.pylab_gui_select is None:

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\IPython\core\pylabtools.py:338, in find_gui_and_backend(gui, gui_select)
    321 def find_gui_and_backend(gui=None, gui_select=None):
    322     """Given a gui string return the gui and mpl backend.
    323 
    324     Parameters
   (...)
    335     'WXAgg','Qt4Agg','module://matplotlib_inline.backend_inline','agg').
    336     """
--> 338     import matplotlib
    340     if _matplotlib_manages_backends():
    341         backend_registry = matplotlib.backends.registry.backend_registry

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\matplotlib\__init__.py:159
    155 from packaging.version import parse as parse_version
    157 # cbook must import matplotlib only within function
    158 # definitions, so it is safe to import from it here.
--> 159 from . import _api, _version, cbook, _docstring, rcsetup
    160 from matplotlib.cbook import sanitize_sequence
    161 from matplotlib._api import MatplotlibDeprecationWarning

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\matplotlib\rcsetup.py:26
     23 import numpy as np
     25 from matplotlib import _api, cbook
---> 26 from matplotlib.backends import BackendFilter, backend_registry
     27 from matplotlib.cbook import ls_mapper
     28 from matplotlib.colors import Colormap, is_color_like

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\matplotlib\backends\__init__.py:1
----> 1 from .registry import BackendFilter, backend_registry  # noqa: F401
      3 # NOTE: plt.switch_backend() (called at import time) will add a "backend"
      4 # attribute here for backcompat.
      5 _QT_FORCE_QT5_BINDING = False

ModuleNotFoundError: No module named 'matplotlib.backends.registry'

Is this way of setting the style not valid anymore? Do I have to use:

import seaborn as sns
sns.set(style='whitegrid') 

What else can I do?

Updating the progress

After trying the recommended uninstall and reinstall, I got another error telling me it cannot uninstall:

WARNING: Ignoring invalid distribution ~atplotlib (C:\...\Python\Python313\Lib\site-packages) 
Collecting matplotlib==3.10.0   
Using cached matplotlib-3.10.0-cp313-cp313-win_amd64.whl.metadata (11 kB) 
Using cached matplotlib-3.10.0-cp313-cp313-win_amd64.whl (8.0 MB) 
Installing collected packages: matplotlib   
Attempting uninstall: matplotlib     
Found existing installation: matplotlib 3.10.0 error: uninstall-no-record-file  
× Cannot uninstall matplotlib 3.10.0 
╰─> The package's contents are unknown: no RECORD file was found for matplotlib. – 
    hint: You might be able to recover from this via: pip install --force-reinstall --no-deps matplotlib==3.10.0

But trying the offered command returns the same result!

I still do not know what to do with this. So I start with this import now:

%matplotlib inline
import matplotlib.pyplot as plt
import seaborn as sns
sns.set_style("whitegrid")

Again, it results in this long traceback:

---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[1], line 1
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
      2 import matplotlib.pyplot as plt
      3 import seaborn as sns

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\IPython\core\interactiveshell.py:2480, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2478     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2479 with self.builtin_trap:
-> 2480     result = fn(*args, **kwargs)
   2482 # The code below prevents the output from being displayed
   2483 # when using magics with decorator @output_can_be_silenced
   2484 # when the last Python token in the expression is a ';'.
   2485 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\IPython\core\magics\pylab.py:103, in PylabMagics.matplotlib(self, line)
     98     print(
     99         "Available matplotlib backends: %s"
    100         % _list_matplotlib_backends_and_gui_loops()
    101     )
    102 else:
--> 103     gui, backend = self.shell.enable_matplotlib(args.gui)
    104     self._show_matplotlib_backend(args.gui, backend)

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\IPython\core\interactiveshell.py:3665, in InteractiveShell.enable_matplotlib(self, gui)
   3662     import matplotlib_inline.backend_inline
   3664 from IPython.core import pylabtools as pt
-> 3665 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
   3667 if gui != None:
   3668     # If we have our first gui selection, store it
   3669     if self.pylab_gui_select is None:

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\IPython\core\pylabtools.py:338, in find_gui_and_backend(gui, gui_select)
    321 def find_gui_and_backend(gui=None, gui_select=None):
    322     """Given a gui string return the gui and mpl backend.
    323 
    324     Parameters
   (...)
    335     'WXAgg','Qt4Agg','module://matplotlib_inline.backend_inline','agg').
    336     """
--> 338     import matplotlib
    340     if _matplotlib_manages_backends():
    341         backend_registry = matplotlib.backends.registry.backend_registry

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\matplotlib\__init__.py:159
    155 from packaging.version import parse as parse_version
    157 # cbook must import matplotlib only within function
    158 # definitions, so it is safe to import from it here.
--> 159 from . import _api, _version, cbook, _docstring, rcsetup
    160 from matplotlib.cbook import sanitize_sequence
    161 from matplotlib._api import MatplotlibDeprecationWarning

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\matplotlib\rcsetup.py:26
     23 import numpy as np
     25 from matplotlib import _api, cbook
---> 26 from matplotlib.backends import BackendFilter, backend_registry
     27 from matplotlib.cbook import ls_mapper
     28 from matplotlib.colors import Colormap, is_color_like

File ~\AppData\Local\Programs\Python\Python313\Lib\site-packages\matplotlib\backends\__init__.py:1
----> 1 from .registry import BackendFilter, backend_registry  # noqa: F401
      3 # NOTE: plt.switch_backend() (called at import time) will add a "backend"
      4 # attribute here for backcompat.
      5 _QT_FORCE_QT5_BINDING = False

ModuleNotFoundError: No module named 'matplotlib.backends.registry'

Then, by calling plt like this, I get this other traceback telling me plt is not defined:

fig = plt.figure()
ax = plt.axes()

Obviously, the previous error didn't let it to be imported correctly, as the error implies:

---------------------------------------------------------------------------
NameError                                 Traceback (most recent call last)
Cell In[2], line 1
----> 1 fig = plt.figure()
      2 ax = plt.axes()

NameError: name 'plt' is not defined

Solution

  • The last thing I did was manually deleting all the residual files and remnanats from the previous installations. All the files named matplotlib, mpl_toolkits, ~atplotlib, or similar in the C:\Users\asus\AppData\Local\Programs\Python\Python313\Lib\site-packages folder had to go to restart with a clean slate. Then reinstall packages with:

    pip install --upgrade ipython jupyter matplotlib
    

    Now these work seamlessly:

    %matplotlib inline
    import matplotlib.pyplot as plt
    import seaborn as sns
    sns.set_style("whitegrid")
    import numpy as np
    fig = plt.figure()
    ax = plt.axes()