Python 从 matplotlib.backends 导入 _tkagg 导入错误:无法导入名称 _tkagg

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/32188180/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-19 11:11:18  来源:igfitidea点击:

from matplotlib.backends import _tkagg ImportError: cannot import name _tkagg

pythonmatplotlibpipvirtualenvtk

提问by QPTR

While trying to run thisexample to test how matplotlib works with Tkinter, I am getting the error:

在尝试运行示例以测试 matplotlib 如何与 Tkinter 一起工作时,我收到错误消息:

(env)fieldsofgold@fieldsofgold-VirtualBox:~/new$ python test.py
Traceback (most recent call last):
  File "test.py", line 7, in <module>
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/matplotlib/backends/backend_tkagg.py", line 13, in <module>
    import matplotlib.backends.tkagg as tkagg
  File "/home/fieldsofgold/new/env/local/lib/python2.7/site-packages/matplotlib/backends/tkagg.py", line 7, in <module>
    from matplotlib.backends import _tkagg
ImportError: cannot import name _tkagg

Using the solution provided here, I've tried to uninstall matplotlib and install the tk and tk-dev packages by using these commands :

使用此处提供的解决方案,我尝试使用以下命令卸载 matplotlib 并安装 tk 和 tk-dev 软件包:

sudo apt-get install tk8.5
sudo apt-get install tk-dev

and then re-installing matplotlib again by pip install matplotlibbut I am still getting the same error. Any help would be appreciated. I am using Ubuntu 14.04 on VirtualBox and working inside a virtualenv environment. Thanks so much.

然后再次重新安装 matplotlib,pip install matplotlib但我仍然遇到相同的错误。任何帮助,将不胜感激。我在 VirtualBox 上使用 Ubuntu 14.04 并在 virtualenv 环境中工作。非常感谢。

回答by YOBA

Try following this scenario:

尝试遵循以下场景:

sudo apt-get update

sudo apt-get install tk tk-dev

sudo pip uninstall matplotlib 

sudo pip install matplotlib

EDIT:

编辑:

Try:

尝试:

sudo pip uninstall matplotlib 

sudo apt-get install python-matplotlib

EDIT2:

编辑2:

sudo apt-get install tk8.6-dev

sudo apt-get remove python-matplotlib

sudo apt-get install python-matplotlib

回答by lmjohns3

I just ran into this (Ubuntu 15.10 but same idea) and fixed it by:

我刚遇到这个(Ubuntu 15.10,但想法相同)并通过以下方式修复它:

sudo apt-get install tk-dev
pip uninstall -y matplotlib
pip --no-cache-dir install -U matplotlib

I think the third step was the critical one; if the cache is permitted then pipappeared to be just using the previously-built installation of matplotlib.

我认为第三步是关键的一步;如果允许缓存,则pip似乎只是使用先前构建的matplotlib.

You can also manually remove the previously-built matplotlib; on Ubuntu it lives in ~/.cache/pipsomewhere. (I couldn't find a way for pip to clean up its cache unfortunately.)

也可以手动删除之前构建的matplotlib;在 Ubuntu 上,它存在于~/.cache/pip某个地方。(不幸的是,我找不到 pip 清理缓存的方法。)

回答by DrawT

One answer to this questionsuggests the error is due to a missing Visual C++ Redistributable for Visual Studio 2015 . Installing it solved the problem for me.

这个问题的一个答案表明该错误是由于缺少 Visual C++ Redistributable for Visual Studio 2015 造成的。安装它为我解决了问题。

回答by Harry

Python 3.7.4 on Windows 10, replacing NavigationToolbar2TkAgg with NavigationToolbar2Tk works for me.

Windows 10 上的 Python 3.7.4,用 NavigationToolbar2Tk 替换 NavigationToolbar2TkAgg 对我有用。

from matplotlib.backends.backend_tkagg import ( FigureCanvasTkAgg, NavigationToolbar2TkAgg)

from matplotlib.backends.backend_tkagg import ( FigureCanvasTkAgg, NavigationToolbar2Tk)

从 matplotlib.backends.backend_tkagg 导入(FigureCanvasTkAgg, NavigationToolbar2TkAgg)

从 matplotlib.backends.backend_tkagg 导入(FigureCanvasTkAgg, NavigationToolbar2Tk)