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

31,125

Solution 1

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

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.

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

Solution 2

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

Change from:

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

To:

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

Solution 3

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

Solution 4

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:

sudo apt-get install tk8.6-dev

sudo apt-get remove python-matplotlib

sudo apt-get install python-matplotlib
Share:
31,125
QPTR
Author by

QPTR

Updated on October 21, 2020

Comments

  • QPTR
    QPTR over 3 years

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

    (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 :

    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. Any help would be appreciated. I am using Ubuntu 14.04 on VirtualBox and working inside a virtualenv environment. Thanks so much.

  • YOBA
    YOBA over 8 years
    @QPTR Ok, then avoid using pip, use the same package manager, I just made an edit
  • QPTR
    QPTR over 8 years
    Hmm, it uninstalls fine. However, it gives python-matplotlib is already the newest version. 0 upgraded, 0 newly installed, 0 to remove and 515 not upgraded. after sudo apt-get install python-matplotlib . Can import matplotlib fine though from the interpreter. But it still doesn't work.
  • YOBA
    YOBA over 8 years
    Weird, are tk and tk-dev being installed correctly?
  • QPTR
    QPTR over 8 years
    As far as I can tell, yes, because when I try to install them it says tk is already the newest version. tk-dev is already the newest version. Is there any other way to know?
  • YOBA
    YOBA over 8 years
    No, I guess you're right, the only thing I can think of is matplotlib not finding tk when building, in my case it works fine with tk8.6-dev, give it a try: again sudo apt-get install tk8.6-dev, then sudo apt-get remove python-matplotlib , finally sudo apt-get install python-matplotlib
  • QPTR
    QPTR over 8 years
    tk8.6dev or just tk8.6 ? 2 options available when I do an apt-cache search tk8.6
  • YOBA
    YOBA over 8 years
    both if you find them in your repository, 8.5 is not working with matplolib for ubuntu 14.04.
  • QPTR
    QPTR over 8 years
    Not working. Shall I try installing matplotlib with pip?
  • YOBA
    YOBA over 8 years
    Ok, give it a try, it's probably a good idea, make sure you uninstall tk tk-dev tk8.5, matplotlib and leave only tk8.6 and tk8.6dev then reinstall matplotlib with pip
  • QPTR
    QPTR over 8 years
    same error :( Uninstalled tk, tk-dev, tk8.5, then used pip to install matplotlib. Even tried sudo apt-get install python-matplotlib but then the system couldn't find matplotlib. So uninstalled and installed with pip again.
  • DanielSank
    DanielSank over 8 years
    It is not a good idea to sudo pip install packages like this. Use virtualenv.
  • luka5z
    luka5z over 7 years
    I've found it the most convenient to use Ubuntu package and run apt-get install python-matplotlib or python3-matplotlib.