Skip to content

[Bug]: Matplotlib falls back to "headless" backend after a while in ssh -X session #29678

Closed
@erjoalgo

Description

@erjoalgo

Bug summary

I reported this bug to QT developers: https://bugreports.qt.io/browse/PYSIDE-3025 but they believe it may be a usage error from the QT client, in this case matplotlib:

It really sounds like something completely unloads the QtWidgets module and Qt library and reloads them; which Qt does not support. Please get back to the matplotlib developers.

The error appears to happen after a while when working on a ssh -X session, and restarting the ssh -X session by logging out and back in temporarily resolves the issue.

The error I get after a while is:

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

In cbook.py matplotlib this is how matplotlib checks if qt is available:

QtWidgets = (
        sys.modules.get("PyQt6.QtWidgets")
        or sys.modules.get("PySide6.QtWidgets")
        or sys.modules.get("PyQt5.QtWidgets")
        or sys.modules.get("PySide2.QtWidgets")
    )

if QtWidgets and QtWidgets.QApplication.instance():
        return "qt"

In the error condition, QtWidgets.QApplication.instance() is falsey.

I'm not familiar with QT or matplotlib but I'd be happy to add more logging where relevant if it helps to track down this bug.

This is on debian bookworm, matplotlib 3.10.0 installed via conda.

Some qt-related versions:

pyqt                      5.15.10         py313h6a678d5_1  
pyqt5-sip                 12.13.0         py313h5eee18b_1  
pyqt6                     6.8.1                    pypi_0    pypi
pyqt6-qt6                 6.8.2                    pypi_0    pypi
pyqt6-sip                 13.10.0                  pypi_0    pypi
qt-main                   5.15.2              hb6262e9_12  

Code for reproduction

import matplotlib
matplotlib.use('QtAgg')
import matplotlib.pyplot as plt

...

plt.subplots()

Actual outcome

  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

Expected outcome

No error, plot is displayed.

Additional information

No response

Operating system

No response

Matplotlib Version

3.10.0

Matplotlib Backend

QtAgg

Python version

Python 3.13.2

Jupyter version

No response

Installation

conda

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions