简体   繁体   English

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

[英]from matplotlib.backends import _tkagg ImportError: cannot import name _tkagg

While trying to run this example 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 matplotlib but I am still getting the same error.然后通过pip install matplotlib matplotlib 再次重新pip install matplotlib但我仍然遇到相同的错误。 Any help would be appreciated.任何帮助,将不胜感激。 I am using Ubuntu 14.04 on VirtualBox and working inside a virtualenv environment.我在 VirtualBox 上使用 Ubuntu 14.04 并在 virtualenv 环境中工作。 Thanks so much.非常感谢。

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 pip appeared to be just using the previously-built installation of matplotlib .如果允许缓存,那么pip似乎只是使用先前构建的matplotlib安装。

You can also manually remove the previously-built matplotlib;也可以手动删除之前构建的matplotlib; on Ubuntu it lives in ~/.cache/pip somewhere.在 Ubuntu 上,它位于~/.cache/pip某处。 (I couldn't find a way for pip to clean up its cache unfortunately.) (不幸的是,我找不到 pip 清理缓存的方法。)

Python 3.7.4 on Windows 10, replacing NavigationToolbar2TkAgg with NavigationToolbar2Tk works for me. Windows 10 上的 Python 3.7.4,用 NavigationToolbar2Tk 替换 NavigationToolbar2TkAgg 对我有用。

Change from:更改自:

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

To:到:

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

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

One answer to this question suggests the error is due to a missing Visual C++ Redistributable for Visual Studio 2015 . 这个问题的一个答案表明该错误是由于缺少 Visual C++ Redistributable for Visual Studio 2015 。 Installing it solved the problem for me.安装它为我解决了问题。

用 NavigationToolbar2Tk 替换 NavigationToolbar2TkAgg 也适用于 Python 3.8.5 (MacOS Catalina)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM