Search code examples
pythonmatplotlibimporterror

ImportError: cannot import name 'docstring' from 'matplotlib'


Recently, my code involving matplotlib.pyplot suddenly stopped working on all my machines (Ubuntu 22.04 LTS). I tried a simple import and got the following error:

$ python
Python 3.10.12 (main, Jun 11 2023, 05:26:28) [GCC 11.4.0] on linux
Type "help", "copyright", "credits" or "license" for more information.
>>> import matplotlib.pyplot as plt
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "/usr/local/lib/python3.10/dist-packages/matplotlib/pyplot.py", line 66, in <module>
    from matplotlib.figure import Figure, FigureBase, figaspect
  File "/usr/local/lib/python3.10/dist-packages/matplotlib/figure.py", line 43, in <module>
    from matplotlib import _blocking_input, backend_bases, _docstring, projections
  File "/usr/local/lib/python3.10/dist-packages/matplotlib/projections/__init__.py", line 58, in <module>
    from mpl_toolkits.mplot3d import Axes3D
  File "/usr/lib/python3/dist-packages/mpl_toolkits/mplot3d/__init__.py", line 1, in <module>
    from .axes3d import Axes3D
  File "/usr/lib/python3/dist-packages/mpl_toolkits/mplot3d/axes3d.py", line 23, in <module>
    from matplotlib import _api, cbook, docstring, _preprocess_data
ImportError: cannot import name 'docstring' from 'matplotlib' (/usr/local/lib/python3.10/dist-packages/matplotlib/__init__.py)

I am not sure what caused the problem, and how to diagnose or fix it. The matplotlib package is installed using pip as root, as I need it to be available to all users by default.

Has anyone encountered a similar issue and know how to fix it?


Solution

  • As pointed out in comments by @Imsteffan, and the linked bug reports here and here, the issue happens because:

    ... docstring was removed after a deprecation cycle of 2 releases and changed to be private (_docstring)

    However, the line that is erroring in mplot3d/axes3d.py was updated #22148, indicating that you have a version of mpl that it is picking up that is <3.6

    This is the case in my OS. It turns out that the apt package python3-matplotlib is also installed as the dependency of another package (qgis). So there are two versions of matplotlib, from pip and from apt, respectively.

    The solution is to remove one of the two versions.

    In the linked bug report, the suggestion is to remove the apt version:

    sudo apt remove python3-matplotlib
    

    But in my case, I can't remove the application that depends on the apt version. So I removed the pip version instead. And then the import works as expected.

    $ sudo pip uninstall matplotlib
    Found existing installation: matplotlib 3.8.0
    Uninstalling matplotlib-3.8.0:
      Would remove:
        /usr/local/lib/python3.10/dist-packages/matplotlib-3.8.0.dist-info/*
        /usr/local/lib/python3.10/dist-packages/matplotlib/*
        /usr/local/lib/python3.10/dist-packages/mpl_toolkits/axes_grid1/*
        /usr/local/lib/python3.10/dist-packages/mpl_toolkits/axisartist/*
        /usr/local/lib/python3.10/dist-packages/mpl_toolkits/mplot3d/*
        /usr/local/lib/python3.10/dist-packages/pylab.py
    Proceed (Y/n)? 
      Successfully uninstalled matplotlib-3.8.0