Skip to content

[Bug]: Working with PyQt5, the different import order will make different result. #21998

Closed

Description

@MusYi

Bug summary

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

import sys

# program run correctly, if import PyQt5 here before import matplotlib
# from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget 
from matplotlib.backends.backend_qt5agg import FigureCanvasQTAgg as FigureCanvas
from matplotlib.figure import Figure
# program will raise TypeError, if import import PyQt5 after import matplotlib
from PyQt5.QtWidgets import QApplication, QVBoxLayout, QWidget

if __name__ == "__main__":
    app = QApplication(sys.argv)
    # init figure
    fig = Figure()
    fig_canvas = FigureCanvas(fig)
    # init widget
    layout = QVBoxLayout()
    layout.addWidget(fig_canvas) # TypeError here, if import PyQt5 after import matlpotlib
    win = QWidget()
    win.setLayout(layout)
    # run
    win.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.

Operating system

Windows

Matplotlib Version

3.5.1

Matplotlib Backend

QtAgg

Python version

Python 3.8.10

Installation

pip

Activity

  1. QuLogic commented on Dec 18, 2021

    @QuLogic
    Member

    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.

  2. changed the title [-][Bug]: [/-] [+][Bug]: Working with PyQt5, the different import order will make different result.[/+] on Dec 18, 2021
  3. MusYi commented on Dec 18, 2021

    @MusYi
    Author

    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!

  4. QuLogic commented on Dec 19, 2021

    @QuLogic
    Member

    Ah yes, this would have broken in 3.5, because it added support for both Python wrappers of Qt6.

  5. tacaswell commented on Dec 19, 2021

    @tacaswell
    Member

    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.

  6. added this to the v3.5.2 milestone on Dec 19, 2021
  7. added 3 commits that reference this issue on Dec 19, 2021
    2058ad0
    0c77b9a
    95a2566
  8. added a commit that references this issue on Dec 31, 2021
    7088a98
  9. added a commit that references this issue on Jan 13, 2022
    8a0483d
  10. added a commit that references this issue on Feb 3, 2022
    1ba1d2b
  11. added 2 commits that reference this issue on Feb 10, 2022
    0e21df2
    a41ad29
  12. added 2 commits that reference this issue on Mar 3, 2022
    bcea9eb
    2bc0c1c
  13. weversonmachado commented on Jun 23, 2022

    @weversonmachado

    Obrigado!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions