Dirk Haupt Dirk Haupt - 6 months ago 88
Python Question

ImportError: cannot import name NavigationToolbar2QTAgg

I have a PyQt4 app project I'd like to turn into an .exe on Windows. I

cd
to the main script and run
pyinstaller SPCanalyse.py
. When I try to run the .exe I get:

C:\Users\user\Documents\BMDanalyse\BMDanalyse\BMDanalyse>C:\Users\user\Documents
\BMDanalyse\BMDanalyse\BMDanalyse\dist\SPCanalyse\SPCanalyse.exe
Traceback (most recent call last):
File "<string>", line 12, in <module>
ImportError: cannot import name NavigationToolbar2QTAgg
SPCanalyse returned -1


Reading this I did try to add from
matplotlib.backends.backend_qt4agg import NavigationToolbar2QT
to the top of
SPCanalyse.py
but to no avail. Though I'm surprised I still have the error since it looks like they fixed it.

I have matplotlib version 1.5.1 and I did find that NavigationToolbar2QTAgg was removed in this version which I'm certain has to do with why I get the error. So if it was removed, why does my code try to import it? How do I prevent this attempted import?

Note that I am having zero problems running my app on Ubuntu.

Cmd output when running pyinstaller:

D:\Home\Downloads\BMDanalyse\BMDanalyse>pyinstaller SPCanalyse.py
49 INFO: PyInstaller: 3.1.1
49 INFO: Python: 2.7.11
49 INFO: Platform: Windows-7-6.1.7601-SP1
49 INFO: wrote D:\Home\Downloads\BMDanalyse\BMDanalyse\SPCanalyse.spec
49 INFO: UPX is not available.
49 INFO: Extending PYTHONPATH with paths
['D:\\Home\\Downloads\\BMDanalyse',
'D:\\Home\\Downloads\\BMDanalyse\\BMDanalyse']
59 INFO: checking Analysis
89 INFO: Building because c:\python27\lib\site-packages\numpy\linalg\__init__.py
changed
89 INFO: Initializing module dependency graph...
99 INFO: Initializing module graph hooks...
119 INFO: running Analysis out00-Analysis.toc
129 INFO: Adding Microsoft.VC90.CRT to dependent assemblies of final executable
required by c:\python27\python.exe
200 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1f
c8b3b9a1e18e3b_9.0.30729.1_none_8550c6b5d18a9128.manifest
200 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1f
c8b3b9a1e18e3b_9.0.30729.4148_none_f47e1bd6f6571810.manifest
200 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1f
c8b3b9a1e18e3b_9.0.30729.4926_none_f47c47b2f658b4a8.manifest
200 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1f
c8b3b9a1e18e3b_9.0.30729.4940_none_f47ed0f6f6564d90.manifest
269 INFO: Searching for assembly x86_Microsoft.VC90.CRT_1fc8b3b9a1e18e3b_9.0.307
29.4940_none ...
279 INFO: Found manifest C:\Windows\WinSxS\Manifests\x86_microsoft.vc90.crt_1fc8
b3b9a1e18e3b_9.0.30729.4940_none_50916076bcb9a742.manifest
279 INFO: Searching for file msvcr90.dll
279 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9
.0.30729.4940_none_50916076bcb9a742\msvcr90.dll
279 INFO: Searching for file msvcp90.dll
279 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9
.0.30729.4940_none_50916076bcb9a742\msvcp90.dll
279 INFO: Searching for file msvcm90.dll
279 INFO: Found file C:\Windows\WinSxS\x86_microsoft.vc90.crt_1fc8b3b9a1e18e3b_9
.0.30729.4940_none_50916076bcb9a742\msvcm90.dll
349 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1f
c8b3b9a1e18e3b_9.0.30729.1_none_8550c6b5d18a9128.manifest
349 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1f
c8b3b9a1e18e3b_9.0.30729.4148_none_f47e1bd6f6571810.manifest
349 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1f
c8b3b9a1e18e3b_9.0.30729.4926_none_f47c47b2f658b4a8.manifest
349 INFO: Found C:\Windows\WinSxS\Manifests\x86_policy.9.0.microsoft.vc90.crt_1f
c8b3b9a1e18e3b_9.0.30729.4940_none_f47ed0f6f6564d90.manifest
349 INFO: Adding redirect Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0,
30729, 4940)
389 INFO: Analyzing D:\Home\Downloads\BMDanalyse\BMDanalyse\SPCanalyse.py
1101 INFO: Processing pre-find module path hook distutils
2987 INFO: Processing pre-safe import module hook _xmlplus
3118 INFO: Processing pre-find module path hook site
3118 INFO: site: retargeting to fake-dir 'c:\\python27\\lib\\site-packages\\PyIn
staller\\fake-modules'
3138 INFO: Processing pre-safe import module hook win32com
4625 INFO: Processing pre-safe import module hook six.moves
7777 INFO: Processing pre-find module path hook PyQt4.uic.port_v3
7787 INFO: Processing pre-find module path hook PyQt4.uic.port_v2
8948 INFO: Looking for import hooks ...
8958 INFO: Processing hook hook-PySide.QtGui.py
9345 INFO: Processing hook hook-distutils.py
9345 INFO: Processing hook hook-sysconfig.py
9345 INFO: Processing hook hook-xml.py
9345 INFO: Processing hook hook-PIL.py
9355 INFO: Excluding import 'FixTk'
9355 INFO: Excluding import 'Tkinter'
9355 INFO: Excluding import 'PyQt5'
9355 WARNING: From PIL.ImageQt removing import PyQt5.qRgba
9355 WARNING: From PIL.ImageQt removing import PyQt5.QPixmap
9355 WARNING: From PIL.ImageQt removing import PyQt5
9355 WARNING: From PIL.ImageQt removing import PyQt5.QImage
9355 INFO: Excluding import 'PySide'
9365 WARNING: From PIL.ImageQt removing import PySide.QtGui
9365 WARNING: From PIL.ImageQt removing import PySide.QtGui.QPixmap
9365 WARNING: From PIL.ImageQt removing import PySide.QtCore
9365 WARNING: From PIL.ImageQt removing import PySide.QtCore.QBuffer
9365 WARNING: From PIL.ImageQt removing import PySide.QtGui.QImage
9365 WARNING: From PIL.ImageQt removing import PySide.QtCore.QIODevice
9365 WARNING: From PIL.ImageQt removing import PySide.QtGui.qRgba
9365 INFO: Excluding import 'PyQt4'
9365 WARNING: From PIL.ImageQt removing import PyQt4.QtGui.QImage
9365 WARNING: From PIL.ImageQt removing import PyQt4.QtCore
9365 WARNING: From PIL.ImageQt removing import PyQt4.QtGui.QPixmap
9365 WARNING: From PIL.ImageQt removing import PyQt4.QtCore.QIODevice
9365 WARNING: From PIL.ImageQt removing import PyQt4.QtCore.QBuffer
9365 WARNING: From PIL.ImageQt removing import PyQt4.QtGui.qRgba
9365 WARNING: From PIL.ImageQt removing import PyQt4.QtGui
9365 INFO: Processing hook hook-httplib.py
9365 INFO: Processing hook hook-pydoc.py
9365 INFO: Excluding import 'Tkinter'
9365 WARNING: From pydoc removing import Tkinter
9375 INFO: Processing hook hook-PyQt4.QtTest.py
9375 INFO: Processing hook hook-PyQt4.py
9375 INFO: Processing hook hook-_tkinter.py
9486 INFO: checking Tree
9527 INFO: checking Tree
9537 INFO: Processing hook hook-matplotlib.backends.py
9978 INFO: Matplotlib backend "GTK": ignored
Gtk* backend requires pygtk to be installed.
10319 INFO: Matplotlib backend "GTKAgg": ignored
Gtk* backend requires pygtk to be installed.
10523 INFO: Matplotlib backend "GTKCairo": ignored
No module named gtk
10843 INFO: Matplotlib backend "MacOSX": ignored
cannot import name _macosx
11184 INFO: Matplotlib backend "Qt4Agg": added
11527 INFO: Matplotlib backend "Qt5Agg": added
11858 INFO: Matplotlib backend "TkAgg": added
12187 INFO: Matplotlib backend "WX": ignored
Matplotlib backend_wx and backend_wxagg require wxPython >=2.8.12
12497 INFO: Matplotlib backend "WXAgg": ignored
Matplotlib backend_wx and backend_wxagg require wxPython >=2.8.12
12710 INFO: Matplotlib backend "GTK3Cairo": ignored
Gtk3 backend requires pygobject to be installed.
13029 INFO: Matplotlib backend "GTK3Agg": ignored
Gtk3 backend requires pygobject to be installed.
13615 INFO: Matplotlib backend "WebAgg": added
13845 INFO: Matplotlib backend "nbAgg": ignored
No module named IPython.display
14167 INFO: Matplotlib backend "agg": added
14496 INFO: Matplotlib backend "cairo": added
14707 INFO: Matplotlib backend "emf": ignored
No module named backend_emf
14917 INFO: Matplotlib backend "gdk": ignored
No module named gobject
15238 INFO: Matplotlib backend "pdf": added
15621 INFO: Matplotlib backend "pgf": added
15965 INFO: Matplotlib backend "ps": added
16290 INFO: Matplotlib backend "svg": added
16621 INFO: Matplotlib backend "template": added
16744 INFO: Processing hook hook-PIL.Image.py
16934 INFO: Processing hook hook-matplotlib.py
17151 INFO: Processing hook hook-PyQt4.uic.py
17161 INFO: Processing hook hook-xml.dom.domreg.py
17161 INFO: Processing hook hook-PyQt4.QtGui.py
17421 INFO: Processing hook hook-pkg_resources.py
17572 INFO: Processing hook hook-PySide.QtCore.py
17644 INFO: Processing hook hook-certifi.py
17644 INFO: Processing hook hook-pytz.py
17714 INFO: Processing hook hook-pywintypes.py
17881 INFO: Processing hook hook-setuptools.py
17881 INFO: Processing hook hook-PIL.SpiderImagePlugin.py
17881 INFO: Excluding import 'FixTk'
17881 INFO: Excluding import 'Tkinter'
17881 INFO: Processing hook hook-encodings.py
18141 INFO: Processing hook hook-PyQt4.QtSvg.py
18141 INFO: Processing hook hook-xml.etree.cElementTree.py
18141 INFO: Processing hook hook-PySide.py
18141 INFO: Processing hook hook-PyQt4.QtCore.py
18201 INFO: Processing hook hook-PyQt4.QtOpenGL.py
18201 INFO: Processing hook hook-win32com.py
18261 INFO: Processing hook hook-pythoncom.py
18471 INFO: Looking for ctypes DLLs
18491 INFO: Analyzing run-time hooks ...
18501 INFO: Including run-time hook 'pyi_rth_pkgres.py'
18501 INFO: Including run-time hook 'pyi_rth_qt4plugins.py'
18511 INFO: Including run-time hook 'pyi_rth_qt4plugins.py'
18511 INFO: Including run-time hook 'pyi_rth__tkinter.py'
18511 INFO: Including run-time hook 'pyi_rth_win32comgenpy.py'
18511 INFO: Including run-time hook 'pyi_rth_mplconfig.py'
18511 INFO: Including run-time hook 'pyi_rth_mpldata.py'
18532 INFO: Looking for dynamic libraries
18922 INFO: Searching for assembly x86_Microsoft.VC90.MFC_1fc8b3b9a1e18e3b_9.0.2
1022.8_none ...
18922 INFO: Found manifest c:\python27\lib\site-packages\Pythonwin\Microsoft.VC9
0.MFC.manifest
18922 INFO: Searching for file mfc90.dll
18922 INFO: Found file c:\python27\lib\site-packages\Pythonwin\mfc90.dll
18922 INFO: Searching for file mfc90u.dll
18922 INFO: Found file c:\python27\lib\site-packages\Pythonwin\mfc90u.dll
18922 INFO: Searching for file mfcm90.dll
18922 INFO: Found file c:\python27\lib\site-packages\Pythonwin\mfcm90.dll
18922 INFO: Searching for file mfcm90u.dll
18922 INFO: Found file c:\python27\lib\site-packages\Pythonwin\mfcm90u.dll
18993 INFO: Adding redirect Microsoft.VC90.MFC version (9, 0, 21022, 8) -> (9, 0
, 21022, 8)
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PyQt4.QtCore
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PyQt4.QtTest
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PyQt4.QtGui
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PyQt4.QtOpenGL
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PyQt4.QtSvg
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PySide.QtCore
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PySide.shiboken
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PySide.QtTest
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PySide.QtOpenGL
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PySide.QtSvg
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PySide.QtNetwork
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PySide.QtGui
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PIL._webp
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PIL._imagingtk
19713 WARNING: Attempted to add Python module twice with different upper/lowerca
ses: PIL._imaging
19713 INFO: Looking for eggs
19713 INFO: Using Python library C:\Windows\system32\python27.dll
19713 INFO: Found binding redirects:
[BindingRedirect(name=u'Microsoft.VC90.CRT', language=None, arch=u'x86', oldVers
ion=(9, 0, 21022, 8), newVersion=(9, 0, 30729, 4940), publicKeyToken=u'1fc8b3b9a
1e18e3b'), BindingRedirect(name=u'Microsoft.VC90.MFC', language=None, arch=u'x86
', oldVersion=(9, 0, 21022, 8), newVersion=(9, 0, 21022, 8), publicKeyToken=u'1f
c8b3b9a1e18e3b')]
19733 INFO: Warnings written to D:\Home\Downloads\BMDanalyse\BMDanalyse\build\SP
Canalyse\warnSPCanalyse.txt
20092 INFO: checking PYZ
20092 INFO: Building because toc changed
20102 INFO: Building PYZ (ZlibArchive) D:\Home\Downloads\BMDanalyse\BMDanalyse\b
uild\SPCanalyse\out00-PYZ.pyz
21024 INFO: checking PKG
21024 INFO: Building because toc changed
21024 INFO: Building PKG (CArchive) out00-PKG.pkg
21056 INFO: Bootloader c:\python27\lib\site-packages\PyInstaller\bootloader\Wind
ows-32bit\run.exe
21056 INFO: checking EXE
21057 INFO: Building because out00-EXE.toc is bad
21059 INFO: Building EXE from out00-EXE.toc
21059 INFO: Appending archive to EXE D:\Home\Downloads\BMDanalyse\BMDanalyse\bui
ld\SPCanalyse\SPCanalyse.exe
21065 INFO: checking COLLECT
WARNING: The output directory "D:\Home\Downloads\BMDanalyse\BMDanalyse\dist\SPCa
nalyse" and ALL ITS CONTENTS will be REMOVED! Continue? (y/n)y
24730 INFO: Removing dir D:\Home\Downloads\BMDanalyse\BMDanalyse\dist\SPCanalyse

25009 INFO: Building COLLECT out00-COLLECT.toc
25029 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25159 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25159 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\pyside.qtcore.pyd
25159 INFO: Updating resource type 24 name 2 language 1033
25180 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25180 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\pyside.shiboken.pyd
25180 INFO: Updating resource type 24 name 2 language 1033
25190 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25190 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\pyside.qttest.pyd
25200 INFO: Updating resource type 24 name 2 language 1033
25210 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25210 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\pyside.qtopengl.pyd
25210 INFO: Updating resource type 24 name 2 language 1033
25220 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25230 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\pyside.qtsvg.pyd
25230 INFO: Updating resource type 24 name 2 language 1033
25240 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25240 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\pyside.qtnetwork.pyd
25240 INFO: Updating resource type 24 name 2 language 1033
25580 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25580 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\pyside.qtgui.pyd
25580 INFO: Updating resource type 24 name 2 language 1033
25661 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25661 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\_elementtree.pyd
25661 INFO: Updating resource type 24 name 2 language 1033
25740 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25740 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\pyside-python2.7.dll
25740 INFO: Updating resource type 24 name 2 language 1033
25750 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25750 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\shiboken-python2.7.dll
25760 INFO: Updating resource type 24 name 2 language 1033
25780 INFO: Redirecting Microsoft.VC90.CRT version (9, 0, 21022, 8) -> (9, 0, 30
729, 4940)
25780 INFO: Updating manifest in C:\Users\Cornelis\AppData\Roaming\pyinstaller\b
incache00_py27_32bit\qtnetwork4.dll
25780 INFO: Updating resource type 24 name 2 language 1033

D:\Home\Downloads\BMDanalyse\BMDanalyse>

Answer

So I've done this a million times now. When you see this:

C:\Python27\python.exe C:/Users/user/PycharmProjects/BMDanalyse/BMDanalyse/SPCanalyse.py
Traceback (most recent call last):
  File "C:/Users/user/PycharmProjects/BMDanalyse/BMDanalyse/SPCanalyse.py", line 14, in <module>
    from pyqtgraph.widgets.MatplotlibWidget import MatplotlibWidget
  File "C:\Python27\lib\site-packages\pyqtgraph\widgets\MatplotlibWidget.py", line 8, in <module>
    from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar
ImportError: cannot import name NavigationToolbar2QTAgg

Go to the offending line in MatplotlibWidget.py and change

from matplotlib.backends.backend_qt4agg import NavigationToolbar2QTAgg as NavigationToolbar

to this:

from matplotlib.backends.backend_qt4agg import NavigationToolbar2QT as NavigationToolbar

Yes, you're changing backend code permanently for this fix. I've not had any problems after making this change. NavigationToolbar2QTAgg was removed in matplotlib 1.5.1