导入错误:无法从“matplotlib”导入名称“docstring”

问题描述 投票:0回答:2

最近,我涉及

matplotlib.pyplot
的代码突然停止在我的所有机器(Ubuntu 22.04 LTS)上运行。我尝试了一个简单的
import
并收到以下错误:

$ 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)

我不确定是什么原因导致了问题,以及如何诊断或修复它。

matplotlib
软件包是使用 pip 作为 root 来安装的,因为我需要它默认对所有用户可用。

有人遇到过类似的问题并且知道如何解决吗?

python matplotlib importerror
2个回答
10
投票

正如@Imsteffan 的评论中所指出的,以及链接的错误报告这里,出现此问题的原因是:

...文档字符串在 2 个版本的弃用周期后被删除, 更改为私有(_docstring)

但是,mplot3d/axes3d.py 中出错的行已更新 #22148,表明您有一个正在拾取的 mpl 版本,即 <3.6

我的操作系统就是这种情况。事实证明,apt 包

python3-matplotlib
也作为另一个包(qgis)的依赖项安装。所以 matplotlib 有两个版本,分别来自 pip 和 apt。

解决方案是删除两个版本之一。

在链接的错误报告中,建议删除 apt 版本:

sudo apt remove python3-matplotlib

但就我而言,我无法删除依赖于apt版本的应用程序。所以我删除了 pip 版本。然后导入将按预期进行。

$ 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

0
投票

目前EnMapBox是从环境安装的 (请参阅https://enmap-box.readthedocs.io/en/latest/usr_section/usr_installation.html#option-a-install-qgis-via-the-official-repository

,在尝试修复时我发现以下问题:

(base) alobo@pop-os:~$ source ~/.virtualenvs/enmapbox/bin/activate
(enmapbox) (base) alobo@pop-os:~$ sudo pip uninstall matplotlib
Found existing installation: matplotlib 3.5.1
Not uninstalling matplotlib at /usr/lib/python3/dist-packages, outside environment /usr
Can't uninstall 'matplotlib'. No files were found to uninstall.
WARNING: Running pip as the 'root' user can result in broken permissions and conflicting behaviour with the system package manager. It is recommended to use a virtual environment instead: https://pip.pypa.io/warnings/venv
© www.soinside.com 2019 - 2024. All rights reserved.