PyQt5 gets “No module named 'PyQt4'” error calling matplotlib.pyplot in Python 3.5

后端 未结 6 1792
春和景丽
春和景丽 2021-02-11 08:19

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:

...
         


        
相关标签:
6条回答
  • 2021-02-11 08:55

    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 讨论(0)
  • 2021-02-11 09:06

    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.

    0 讨论(0)
  • 2021-02-11 09:09

    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).

    0 讨论(0)
  • 2021-02-11 09:17

    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.

    0 讨论(0)
  • 2021-02-11 09:18

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

    0 讨论(0)
  • 2021-02-11 09:20

    3 steps can solve this problem:

    pip uninstall pyqt5
    pip uninstall matplotlib
    pip install matplotlib
    
    0 讨论(0)
提交回复
热议问题