You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
[Bug]: Working with PyQt5, the different import order will make different result. #21998
Working with PyQt5, the different import order will make different result.
If import PyQt5 before import matplotlib, the program will run normally.
If import PyQt5 after import matplotlib, the program will riase TypeError at the method of QLayout.addWidget.
Code for reproduction
importsys# program run correctly, if import PyQt5 here before import matplotlib# from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget frommatplotlib.backends.backend_qt5aggimportFigureCanvasQTAggasFigureCanvasfrommatplotlib.figureimportFigure# program will raise TypeError, if import import PyQt5 after import matplotlibfromPyQt5.QtWidgetsimportQApplication, QVBoxLayout, QWidgetif__name__=="__main__":
app=QApplication(sys.argv)
# init figurefig=Figure()
fig_canvas=FigureCanvas(fig)
# init widgetlayout=QVBoxLayout()
layout.addWidget(fig_canvas) # TypeError here, if import PyQt5 after import matlpotlibwin=QWidget()
win.setLayout(layout)
# runwin.show()
sys.exit(app.exec())
Actual outcome
TypeError
addWidget(self, QWidget, stretch: int = 0, alignment: Union[Qt.Alignment, Qt.AlignmentFlag] = Qt.Alignment()): argument 1 has unexpected type 'FigureCanvasQTAgg'
File "E:\CODE\Python\testroom.py", line 17, in <module>
layout.addWidget(fig_canvas)
Expected outcome
expect no TypeError
Additional information
This Bug will not happen at the version v3.4.x of matplotlib.
I am going to re-open this, even though you can work around this by changing the import order, we should not have that sort of brittleness. I'm working on a PR to fix this.
Activity
QuLogic commentedon Dec 18, 2021
Both ways work fine for me; are you certain that Matplotlib loaded PyQt5 and not PySide2?
See this explanation, but the safest way to guarantee that Matplotlib loads the intended Qt wrapper is to import it first.
[-][Bug]: [/-][+][Bug]: Working with PyQt5, the different import order will make different result.[/+]MusYi commentedon Dec 18, 2021
Thanks, I got it.
I tried it in a new python environment which just install matplotlib and pyqt5, the program will run correctlly.
So I checked the python pip environment which cause the TypeError, it contains both pyqt5 and pyside6.
At that time the matplotlib automatically import pyside6, which will conflict with the later imported pyqt5 and generate an error.
Thanks again!
QuLogic commentedon Dec 19, 2021
Ah yes, this would have broken in 3.5, because it added support for both Python wrappers of Qt6.
tacaswell commentedon Dec 19, 2021
I am going to re-open this, even though you can work around this by changing the import order, we should not have that sort of brittleness. I'm working on a PR to fix this.
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
FIX: ensure that qt5agg and qt5cairo backends actually use qt5
runtime: Import PyQt5 before matplotlib to work around a bug
runtime: Import PyQt5 before matplotlib to work around a bug
runtime: Import PyQt5 before matplotlib to work around a bug
runtime: Import PyQt5 before matplotlib to work around a bug
runtime: Import PyQt5 before matplotlib to work around a bug
runtime: Import PyQt5 before matplotlib to work around a bug
weversonmachado commentedon Jun 23, 2022
Obrigado!
runtime: Import PyQt5 before matplotlib to work around a bug