Uploaded image for project: 'Qt for Python'
  1. Qt for Python
  2. PYSIDE-3025

In ssh -X, QtWidgets.QApplication.instance() becomes None after a while

    XMLWordPrintable

Details

    • Bug
    • Resolution: Out of scope
    • Not Evaluated
    • None
    • 6.8.2
    • Other
    • None
    • o LSB modules are available.
      Distributor ID: Debian
      Description: Debian GNU/Linux 12 (bookworm)
      Release: 12
      Codename: bookworm
      (6.86x)
    • Linux/X11

    Description

      I'm using matplotlib with python, which relies on QT for displaying plots. I'm logged into a remote workstation, using ssh -X. I'm running a python file which imports matplotlib and displays some plots on the screen.

       

      The plots work well for some time, but after a while and seemingly at random, the plots stop working. Without changing anything, I suddenly start seeing errors like:

       

      ImportError: Cannot load backend 'QtAgg' which requires the 'qt' interactive framework, as 'headless' is currently running
      

       

      This magically resolves itself by exiting the `ssh X` session and logging in again.

       

      I tried adding some logging to try to understand why matplotlib thinks the current backend is "headless" instead of "qt", this is what I found:

       

      
      QtWidgets = (
              sys.modules.get("PyQt6.QtWidgets")
              or sys.modules.get("PySide6.QtWidgets")
              or sys.modules.get("PyQt5.QtWidgets")
              or sys.modules.get("PySide2.QtWidgets")
          )
          print("DDEBUG cbook.py vgk3: value of QtWidgets: {}".format(QtWidgets))
          instance = QtWidgets.QApplication.instance()
          print("DDEBUG cbook.py 8y0p: value of instance: {}".format(instance))
          if QtWidgets and QtWidgets.QApplication.instance():
              return "qt"
      
      
      DDEBUG cbook.py vgk3: value of QtWidgets: <module 'PyQt6.QtWidgets' from '/home/USER/anaconda3/envs/6.86x/lib/python3.13/site-packages/PyQt6/QtWidgets.abi3.so'>
      DDEBUG cbook.py 8y0p: value of instance: None
      DDEBUG pyplot.py n57x: value of current_framework: headless
      DDEBUG pyplot.py fr4q: value of required_framework: qt
      Traceback (most recent call last):
        File "/home/USER/git/6.86x/project1/sentiment_analysis/./main.py", line 55, in <module>
          plot_toy_results('Perceptron', thetas_perceptron)
          ~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/USER/git/6.86x/project1/sentiment_analysis/./main.py", line 53, in plot_toy_results
          utils.plot_toy_data(algo_name, toy_features, toy_labels, thetas)
          ~~~~~~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/USER/git/6.86x/project1/sentiment_analysis/utils.py", line 102, in plot_toy_data
          plt.subplots()
          ~~~~~~~~~~~~^^
        File "/home/USER/anaconda3/envs/6.86x/lib/python3.13/site-packages/matplotlib/pyplot.py", line 1777, in subplots
          fig = figure(**fig_kw)
        File "/home/USER/anaconda3/envs/6.86x/lib/python3.13/site-packages/matplotlib/pyplot.py", line 1044, in figure
          manager = new_figure_manager(
              num, figsize=figsize, dpi=dpi,
              facecolor=facecolor, edgecolor=edgecolor, frameon=frameon,
              FigureClass=FigureClass, **kwargs)
        File "/home/USER/anaconda3/envs/6.86x/lib/python3.13/site-packages/matplotlib/pyplot.py", line 553, in new_figure_manager
          _warn_if_gui_out_of_main_thread()
          ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
        File "/home/USER/anaconda3/envs/6.86x/lib/python3.13/site-packages/matplotlib/pyplot.py", line 530, in _warn_if_gui_out_of_main_thread
          canvas_class = cast(type[FigureCanvasBase], _get_backend_mod().FigureCanvas)
                                                      ~~~~~~~~~~~~~~~~^^
        File "/home/USER/anaconda3/envs/6.86x/lib/python3.13/site-packages/matplotlib/pyplot.py", line 369, in _get_backend_mod
          switch_backend(rcParams._get("backend"))
          ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
        File "/home/USER/anaconda3/envs/6.86x/lib/python3.13/site-packages/matplotlib/pyplot.py", line 435, in switch_backend
          raise ImportError(
          ...<2 lines>...
                  newbackend, required_framework, current_framework))
      ImportError: Cannot load backend 'QtAgg' which requires the 'qt' interactive framework, as 'headless' is currently running
      

       

      matplotlib's cbook.py checks that a QT module can be imported, and that QtWidgets.QApplication.instance() is truthy. When this bug is being reproduced, the latter is false, i.e. QtWidgets.QApplication.instance() is None.

       

      I'm not sure if I can dig down any further since I'm not familiar with QT internals and the _file_ for QT modules in python refer to .so libraries.

       

      What further debugging steps do you recommend for helping us understand why QtWidgets.QApplication.instance() mysteriously becomes None after a while within an ssh -X session?

      Attachments

        No reviews matched the request. Check your Options in the drop-down menu of this sections header.

        Activity

          People

            crmaurei Cristian Maureira-Fredes
            erjoalgo Ernesto Alfonso
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

            Dates

              Created:
              Updated:
              Resolved:

              Gerrit Reviews

                There are no open Gerrit changes