从'matplotlib'无法导入'_png'名称

19

我想使用matplotlib替代kivy_garden.graph。实际上,我尝试了这段代码,以检查它是否适合我。我在安装matplotlib时遇到了一些问题,但已经成功(或者没有)解决了。

当我开始运行代码时,我遇到了from matplotlib import _png ImportError: cannot import name '_png' from 'matplotlib' (D:\PyCharmProjects\kivyApp\venv\lib\site-packages\matplotlib\__init__.py)的错误。 我重新安装了matplotlib和pip,尝试了另一个版本的matplotlib,但我不知道为什么它对我没有起作用。 我使用的是Python 3.7.5pip 20.2.4matplotlib 3.3.3


看起来你的项目是安装在虚拟环境中的。在安装matplotlib之前,你应该激活该虚拟环境。具体的操作指南取决于你的操作系统和特定的shell,无论是cmd.exe还是powershell。 - Mark
9个回答

29

我尝试回滚到matplotlib版本3.0.2没有成功,但是使用3.1.3成功了。

python -m pip uninstall matplotlib
pip install matplotlib==3.1.3

Python 3.8.2

->

Python 3.8.2


3
这是什么样的解决方案?一个合适的回答需要针对当前稳定版本的Matplotlib提供可行的解决方案。 - Martin

6

我在使用Google Colab时遇到了这个问题,一直无法解决。后来我发现一个简单的解决方案是安装稳定版本的matplotlib,即pip install -U matplotlib,然后重新启动运行时就可以了。


2

我在Google Colab中也遇到了这个问题

尝试使用 pip install matplotlib==3.1.3 这个命令可以解决我的问题。


1
现在它可以工作了。我在PyCharm终端中执行了py -m pip uninstall matplotlib,然后执行了py -m pip install matplotlib --version=3.0.2。在cmd和git bash中执行相同的命令无法正常工作。请注意保留HTML标签。

1
只需导入matplotlib库。
pip install matplotlib==3.1.3

而且,这个版本。

0

由于这个错误存在了这么多年我放弃了等待kivy-garden社区来修复它。所以当部署我的基于kivy的软件时,我也分发以下的临时解决方案。它会自动找到文件并注释掉那一行。

from pathlib import Path
# Join user's home directory with Garden's file path
file = Path.home().joinpath('.kivy/garden/garden.matplotlib/backend_kivy.py')
# Read file
lines = []
with open(file, 'r') as f:
    for line in f:
        lines.append(line)
# Write file
with open(file, 'w') as f:
    for line in lines:
        if line.startswith('from matplotlib import _png'):
            # comment out this troublesome line
            line = '#' + line
        f.write(line)

0

如果您正在使用 Linux 发行版,则问题在于 matplotlib 的安装。

卸载当前版本: pip uninstall matplotlib

正确的安装可以在以下网址找到: https://matplotlib.org/stable/users/installing.html

从下面选择适合您发行版的正确代码:

Linux 包管理器 如果您正在使用与您的 Linux 发行版捆绑的 Python 版本,则可以通过您的包管理器安装 Matplotlib,例如:

Debian / Ubuntu:sudo apt-get install python3-matplotlib

Fedora:sudo dnf install python3-matplotlib

Red Hat:sudo yum install python3-matplotlib

Arch:sudo pacman -S python-matplotlib

操作完成后应该可以正常工作。


0

你也可以在这个导入的行上添加注释 :) (对我来说,它是C:\Users\{username}\.kivy\garden\garden.matplotlib\backend_kivy.py的第256行) 但我不知道它可能会在未来带来什么问题。


这对我来说似乎是一种不安全的黑客方式,但记录下来仍然很有趣。在使用这个库的代码中,经过这样做后,它还能正常工作吗? - joanis

0

我曾经遇到过这个问题,并且可以确认升级到最新的matplotlib版本解决了这个问题:

pip install --upgrade matplotlib
pip show matplotlib

Name: matplotlib
Version: 3.6.2
Summary: Python plotting package
Home-page: https://matplotlib.org
Author: John D. Hunter, Michael Droettboom
Author-email: matplotlib-users@python.org
License: PSF
Location: /usr/local/lib/python3.8/dist-packages
Requires: python-dateutil, pyparsing, pillow, numpy, fonttools, cycler, contourpy, kiwisolver, packaging
Required-by: yellowbrick, wordcloud, weightwatcher, seaborn, scikit-image, pycocotools, prophet, powerlaw, plotnine, pandas-profiling, mlxtend, mizani, missingno, matplotlib-venn, keras-vis, imgaug, fastai, descartes, datascience, daft, arviz

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接