Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PyQt5 and Matplotlib 1.4.2 - installing one breaks the other

I am trying to write a PyQt5 application that embeds a matplotlib plot within it. However, I am having a maddening time where if I install matplotlib PyQt5 breaks due to the interference by PyQt4. This can be seen in this error:

In [2]: from PyQt5 import QtCore, QtGui, QtWidgets
---------------------------------------------------------------------------
RuntimeError                              Traceback (most recent call last)
<ipython-input-2-43848d5bd21e> in <module>()
----> 1 from PyQt5 import QtCore, QtGui, QtWidgets

RuntimeError: the PyQt5.QtCore and PyQt4.QtCore modules both wrap the QObject class

If I remove PyQt4 (and reinstall PyQt5 since removing PyQt4 removes sip) I then have this issue:

In [1]: import matplotlib.backends.backend_qt5agg
---------------------------------------------------------------------------
ImportError                               Traceback (most recent call last)
<ipython-input-1-6d2c21e1d629> in <module>()
----> 1 import matplotlib.backends.backend_qt5agg

C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt5agg.py in <module>()
     16
     17 from .backend_agg import FigureCanvasAgg
---> 18 from .backend_qt5 import QtCore
     19 from .backend_qt5 import QtGui
     20 from .backend_qt5 import FigureManagerQT

C:\Anaconda3\lib\site-packages\matplotlib\backends\backend_qt5.py in <module>()
     29     figureoptions = None
     30
---> 31 from .qt_compat import QtCore, QtGui, QtWidgets, _getSaveFileName, __version__
     32 from matplotlib.backends.qt_editor.formsubplottool import UiSubplotTool
     33

C:\Anaconda3\lib\site-packages\matplotlib\backends\qt_compat.py in <module>()
     89     if QT_API in [QT_API_PYQT, QT_API_PYQTv2]:  # PyQt4 API
     90
---> 91         from PyQt4 import QtCore, QtGui
     92
     93         try:

ImportError: cannot import name 'QtCore'

I have gone through this cycle multiple times, installing each from different sources. I am using the Anaconda distribution of Python 3.4, which I have also uninstalled / reinstalled once already.

I must be doing something wrong, but I honestly can't figure out what it is.

Any assistance would be appreciated

like image 633
dan_g Avatar asked Nov 18 '14 00:11

dan_g


1 Answers

As your matplotlib depends on PyQt4, you need to force Matplotlib to use PyQt5 backend. Like this:

import matplotlib
matplotlib.use("Qt5Agg")

This function must be called before importing pyplot for the first time; or, if you are not using pyplot, it must be called before importing matplotlib.backends.

like image 144
ufbycd Avatar answered Nov 14 '22 17:11

ufbycd