2

I'm trying to run an application that was running before. It used to run on both Ubuntu and Windows 7. I now have a Windows 10 machine, and the code fails. It might be because I'm on python 3.5 now, though I frankly can't remember what I was on on the other machines (which are no longer available).

The key lines are:

import matplotlib
matplotlib.use("TkAgg")
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

which has been lifted from various places some time ago. However, now when I run it, it throws an error:

Traceback (most recent call last):
  File "C:\....py", line 17, in <module>
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
  File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 13, in <module>
    import matplotlib.backends.tkagg as tkagg
  File "C:\Users\g...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\backends\tkagg.py", line 9, in <module>
    from matplotlib.backends import _tkagg
ImportError: DLL load failed: The specified module could not be found.

In an interactive session, it seems that it's the third line that's failing:

>>> import matplotlib
>>> matplotlib.use("TkAgg")
>>> from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
Traceback (most recent call last):
  File "<pyshell#4>", line 1, in <module>
    from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
  File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\backends\backend_tkagg.py", line 13, in <module>
    import matplotlib.backends.tkagg as tkagg
  File "C:\Users\...\AppData\Local\Programs\Python\Python35-32\lib\site-packages\matplotlib\backends\tkagg.py", line 9, in <module>
    from matplotlib.backends import _tkagg
ImportError: DLL load failed: The specified module could not be found.

I tried

pip uninstall matplotlib
pip install matplotlib

in the command prompt, but the error remained.

It seems there may be a python-matplotlib-tk package, but I'm not sure what this is.

Should I drop back to python 3.4, or is there a solution for this? Is this a known problem?


In response to the below comment on installing matplotlib:

C:\>pip install matplotlib
Collecting matplotlib
  Downloading matplotlib-1.5.1-cp35-none-win32.whl (6.2MB)
    100% |################################| 6.2MB 67kB/s
Requirement already satisfied (use --upgrade to upgrade): numpy>=1.6 in c:\users\...\appdata\local\programs\python\python35-32\lib\site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): pytz in c:\users\...\appdata\local\programs\python\python35-32\lib\site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): python-dateutil in c:\users\...\appdata\local\programs\python\python35-32\lib\site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): cycler in c:\users\...\appdata\local\programs\python\python35-32\lib\site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): pyparsing!=2.0.4,>=1.5.6 in c:\users\...\appdata\local\programs\python\python35-32\lib\site-packages (from matplotlib)
Requirement already satisfied (use --upgrade to upgrade): six>=1.5 in c:\users\...\appdata\local\programs\python\python35-32\lib\site-packages (from python-dateutil->matplotlib)
Installing collected packages: matplotlib
Successfully installed matplotlib-1.5.1
Dr Xorile
  • 967
  • 1
  • 7
  • 20
  • Here's a link to a tutorial that uses this method for python 3.4: [here](https://pythonprogramming.net/how-to-embed-matplotlib-graph-tkinter-gui/) – Dr Xorile Mar 04 '16 at 20:54
  • How did you install matplotlib? Did it all compile properly? – MattDMo Mar 04 '16 at 20:57
  • @MattDMo, yes. Please see pip report in the question – Dr Xorile Mar 04 '16 at 21:00
  • Oh, right, I forgot there were Windows wheels on PyPI. Next question: can you successfully `import tkinter`? – MattDMo Mar 04 '16 at 21:06
  • I'm going to try to go back to python 3.4. It seems I also need Microsoft Visual C++ 10.0, which is (as far as I can tell) Visual Studio Community 2015. I'm installing that now. Others have said to try to install numpy from [sourceforge](https://sourceforge.net/projects/numpy/files/NumPy/1.9.2/), which I might try next. – Dr Xorile Mar 05 '16 at 00:01
  • Does anyone have a window 10 machine with python 3.5 and matplotlib and able to run the first three lines I gave above. – Dr Xorile Mar 05 '16 at 00:02
  • Okay, so with python 3.4 and the sourceforge install of numpy and visual studio community 2015, the code now works – Dr Xorile Mar 05 '16 at 00:12

4 Answers4

4

I had the same problem

I read the docs in matplotlib

For Python 3.5 the Visual C++ Redistributable for Visual Studio 2015 needs to be installed. In case Python 2.7 to 3.4 are not installed for all users (not the default), the Microsoft Visual C++ 2008 ( 64 bit or 32 bit for Python 2.7 to 3.2) or Microsoft Visual C++ 2010 ( 64 bit or 32 bit for Python 3.3 and 3.4) redistributable packages need to be installed.

And I installed the Visual C++ Redistributable for Visual Studio 2015 It solved the problem

Smart Manoj
  • 5,230
  • 4
  • 34
  • 59
3

For those who just recently ran into this problem, I found the redistributable solution to not work, as I had both 32bit and 64bit redistributables already installed.

The solution I found was to replace NavigationToolbar2TkAgg with NavigationToolbar2Tk.

1

I've also found a way to import the packages, using 'import matplotlib as mpl':

       import matplotlib as mpl
       mpl.use("TkAgg")
       from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg

This works fine for me, hopefully it works for others too (:

Dorothée
  • 39
  • 6
0

This is your problem:

ImportError: DLL load failed: The specified module could not be found.

You could try opening _tkagg.pyd in a software like Dependency Walker and look for errors.

It's also possible that _tkagg.pyd was built with an incompatible version of the Python you are using

Dimitri Merejkowsky
  • 1,001
  • 10
  • 12