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

后端 未结 6 1793
春和景丽
春和景丽 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.

提交回复
热议问题