1

I am using Anaconda with Python 3.5.2, Matplotlib 2.0.2, PyQt5.6 on a windows 10 machine. When I import matplotlib.pyplot as plt I get the following error:

...
File "C:...\Anaconda3\Lib\site-packages\matplotlib\backends\qt_compat.py", 
           line 137, in <module> from PyQt4 import QtCore, QtGui

ImportError: No module named 'PyQt4'

I don't know why it would want to import from PyQt4 when it has never been installed on my machine.

This question has the same error, but on a machine that actually has PyQt4 installed.

I have checked my matplotlibrc file, I've used matplotlib.use('qt5agg') in the program, also matplotlib.rcParams['backend'] = "Qt5Agg". I've uninstalled and reinstalled all the above packages to no avail. I have even attempted to install PyQt4 just to get things going. I'm completely stumped. None of the various possible causes or remedies that I've been able to find on either SO or github have helped.

d8sconz
  • 279
  • 4
  • 14
  • You need to install PyQt4 with pip. your version of matplotlib requires PyQt4. See http://pyqt.sourceforge.net/Docs/PyQt4/installation.html on how to install – tushortz May 18 '17 at 15:18
  • No, it doesn't make sense to install PyQt4 if PyQt5 should be used. Can you try `import matplotlib; matplotlib.use("Qt5Agg"); matplotlib.rcParams["verbose.level"] = "debug"; import matplotlib.pyplot as plt` and see if further information is given which you can post here? – ImportanceOfBeingErnest May 18 '17 at 15:32
  • The error is the same, essentially. The backend actually gets set to qt5agg.py but then as above, It starts looking for qt4 after the call to qt_compat.py. – d8sconz May 18 '17 at 23:55

6 Answers6

1

I just had the same error (even though on Windows 7). For me the problem was that there was an old matplotlibrc file in C:\Users\<username>\.matplotlib\matplotlibrc which overwrote the settings from my environment's matplotlibrc file. Deleting that file solved the issue for me.

marcu1000s
  • 177
  • 1
  • 8
1

It seems the API version is PyQt5 but the default is PyQt4,just open Tools -> Preferences -> IPython console -> Graphics -> backend,change QT4 to QT5

Arrow.Tao
  • 33
  • 7
0

Looking at the file and line number in your error it looks that you have the same issue that I answered here.

That is, I think your issue is caused by having a QT_API environment variable that still is set to pyqt4 (or pyside).

Community
  • 1
  • 1
titusjan
  • 5,376
  • 2
  • 24
  • 43
  • Thanks. I get "None" in response to `print(os.environ.get('QT_API'))`. – d8sconz May 18 '17 at 23:43
  • Have set system variable and now get "pyqt5" in response to `print(os.environ.get('QT_API'))`. Error remains though. – d8sconz May 19 '17 at 04:40
  • The way forward is to look in `qt_compat.py` and try to debug why it tries to import PyQt4 at line 137. The best way is to copy `qt_compat.py` to a temporary directory and then, from that directory, start `python -m qt_compat`. This should give you the same error, but now you can add some print statements to the copy of `qt_compat.py` that make it easier to debug the issue. The line above the import says `if QT_API in [QT_API_PYQT, QT_API_PYQTv2]`. Start by printing the value of the `QT_API` global variable at that point. I'm sure you can make some progress from there. – titusjan May 19 '17 at 06:53
  • Now I'm getting, `c:...\qt_compat.py(127)() -> from PyQt5 import QtCore, QtGui, QtWidgets (Pdb) > c:\...\qt_compat.py(128)() -> _getSaveFileName = QtWidgets.QFileDialog.getSaveFileName (Pdb) AttributeError: module 'PyQt5.QtWidgets' has no attribute 'QFileDialog'`. In QtWidgets.pyw there is class QFileDialog. I'm at the limit of my expertise. I'm considering a complete uninstall/reinstall of anaconda. – d8sconz May 19 '17 at 08:20
  • What a mess :-). I must say that I don't have a clue what could cause that last issue. Perhaps a reinstall is a good idea, or you could first try in a new conda environment where you only install what's needed. – titusjan May 19 '17 at 10:52
  • I've gone with the reinstall option but am now having a related issue. Everything was fine until I imported a package that worked perfectly before and I'm getting a similar error to the above:` module blahblah has no attribute...` when, indeed, it does. It's as if Python has lost the ability to look inside the packages or become blind to what is in there. Anyway, a note of thanks for your time on this. – d8sconz May 20 '17 at 01:12
0

I ran into a similar thing and found that the problem was having a 64-bit Anaconda installation that couldn't load the PyPt5 DLLs that were perhaps 32-bit. The short answer is that I uninstalled Anaconda 64-bit and installed the 32-bit version instead.

To work through this, I stepped through qt_compat.py where the error originates. For most of the time through this module, it’s trying to work with PyQt5, as that’s what it finds in the environment. However, when it gets to the lines below, the import fails so it tries to fall back to PyQt4, which isn’t installed with Anaconda, and thus issues the error.

if QT_API == QT_API_PYQT5:
    try:
        from PyQt5 import QtCore, QtGui, QtWidgets
        _getSaveFileName = QtWidgets.QFileDialog.getSaveFileName
    except ImportError:
        # fell through, tried PyQt5, failed fall back to PyQt4
        QT_API = rcParams['backend.qt4']
        QT_RC_MAJOR_VERSION = 4

Testing the import statement outside of the file gave the message “DLL load failed: %1 is not a valid Win32 application.” That's what suggests the DLL mismatch, and also answers why it was trying to fall back to PyQt4.

0

3 steps can solve this problem:

pip uninstall pyqt5
pip uninstall matplotlib
pip install matplotlib
Stephen Rauch
  • 47,830
  • 31
  • 106
  • 135
0

I had faced the same problem and here is how I fixed it.

Look into your matplotlib configuration file which is often located at path-to/site-packages/matplotlib/mpl-data/matplotlibrc or path-to/Anaconda3/envs/your_env_name/Lib/site-packages/matplotlib/mpl-data/matplotlibrc

See an example of matplotlibrc here https://github.com/daler/matplotlibrc/blob/master/rc/default

If you have pyqt5.x.x installed and have the statement 'backend : Qt5Agg' in matplotlibrc, then to use '%matplotlib qt', change '#backend.qt4 : PyQt4' to '#backend.qt4 : PyQt5'.

For an Anaconda environment, reactivate the environment.

Note if you update matplotlib in the future, your matplotlibrc will automatically be overwritten.