Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

FIX: pyplot auto-backend detection case-sensitivity fixup #29721

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Conversation

Copy link
Contributor

@jayaddison jayaddison commented Mar 9, 2025
edited
Loading

PR summary

There seems to be a case-sensitivity bug in the matplotlib.pyplot module code that decides whether to configure backend autodetection, and I think that this is the cause of the bug reported in #29713.

The bug means that a current interactive environment value of TkAgg is not detected as one of the possible values from a set that includes the string tkagg -- and so an auto-backend is not configured, as it would have been previously in v3.8.3 of this library.

A minimal repro example is provided below, for use in a container environment with no graphical/GUI environment available:

>>> import matplotlib.pyplot
>>> matplotlib.pyplot.new_figure_manager()
Traceback (most recent call last):
 File "<python-input-1>", line 1, in <module>
 matplotlib.pyplot.new_figure_manager()
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
 File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 551, in new_figure_manager
 _warn_if_gui_out_of_main_thread()
 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^^
 File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 528, in _warn_if_gui_out_of_main_thread
 canvas_class = cast(type[FigureCanvasBase], _get_backend_mod().FigureCanvas)
 ~~~~~~~~~~~~~~~~^^
 File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 369, in _get_backend_mod
 switch_backend(rcParams._get("backend"))
 ~~~~~~~~~~~~~~^^^^^^^^^^^^^^^^^^^^^^^^^^
 File "/usr/lib/python3/dist-packages/matplotlib/pyplot.py", line 433, in switch_backend
 raise ImportError(
 ...<2 lines>...
 newbackend, required_framework, current_framework))
ImportError: Cannot load backend 'TkAgg' which requires the 'tk' interactive framework, as 'headless' is currently running

PR checklist

  • "closes #0000" is in the body of the PR description to link the related issue
  • new and changed code is tested
  • (削除) Plotting related features are demonstrated in an example (削除ここまで) N/A
  • (削除) New Features and API Changes are noted with a directive and release note (削除ここまで) N/A
  • (削除) Documentation complies with general and docstring guidelines (削除ここまで) N/A

Closes #29713.

Copy link
Contributor Author

Thinking about how to add test coverage here: I think that modifying/extending the test_backend_fallback_headless and test_backend_fallback_headful test cases might be a reasonable way to demonstrate the problem and/or fix:

@pytest.mark.skipif(sys.platform != "linux", reason="Linux only")
def test_backend_fallback_headless(tmp_path):
env = {**os.environ,
"DISPLAY": "", "WAYLAND_DISPLAY": "",
"MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
with pytest.raises(subprocess.CalledProcessError):
subprocess_run_for_testing(
[sys.executable, "-c",
"import matplotlib;"
"matplotlib.use('tkagg');"
"import matplotlib.pyplot;"
"matplotlib.pyplot.plot(42);"
],
env=env, check=True, stderr=subprocess.DEVNULL)
@pytest.mark.skipif(
sys.platform == "linux" and not _c_internal_utils.xdisplay_is_valid(),
reason="headless")
def test_backend_fallback_headful(tmp_path):
if parse_version(pytest.__version__) >= parse_version('8.2.0'):
pytest_kwargs = dict(exc_type=ImportError)
else:
pytest_kwargs = {}
pytest.importorskip("tkinter", **pytest_kwargs)
env = {**os.environ, "MPLBACKEND": "", "MPLCONFIGDIR": str(tmp_path)}
backend = subprocess_run_for_testing(
[sys.executable, "-c",
"import matplotlib as mpl; "
"sentinel = mpl.rcsetup._auto_backend_sentinel; "
# Check that access on another instance does not resolve the sentinel.
"assert mpl.RcParams({'backend': sentinel})['backend'] == sentinel; "
"assert mpl.rcParams._get('backend') == sentinel; "
"assert mpl.get_backend(auto_select=False) is None; "
"import matplotlib.pyplot; "
"print(matplotlib.get_backend())"],
env=env, text=True, check=True, capture_output=True).stdout
# The actual backend will depend on what's installed, but at least tkagg is
# present.
assert backend.strip().lower() != "agg"

Copy link
Contributor Author

It's possible to re-order some of the import and get_backend code lines in test_backend_fallback_headful to get the test to fail without this fix and pass with it in place -- but I'm not completely certain what the expected logic should be, so I'll open this PR for review/feedback.

@jayaddison jayaddison marked this pull request as ready for review March 10, 2025 11:29
Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: I was wondering whether this is the right case to handle the case. Alternatives could be

  • validate_backend() coercing to lower-case. See
    def validate_backend(s):

    While we could do this, the BackendRegistry generally accepts arbitrary casing and coerces internally. Therefore, it may be slightly more consistent to keep the name as is in rcParams, so that the user always sees the name as given: They may use matplotlib.use("TkAgg") and set "TkAgg" in their matplotlibrc file. Then it's slightly awkward to return the lower-case variant for rcParams["backend_fallback"] - though one could still argue that eager normalization is still the way to go. I have no strong opinion here.
  • Push everything down to the BackendRegistry. While this would be nice in principle, we have a very specific request here: "Is the given backend name an available interactive backend, but not webagg or nbagg" and that's not easy to express as a high-level BackendRegistry API that could support arbitrary casing.

I therefore think the present solution is right.

Copy link
Contributor Author

@nileshpatra can you confirm that this fixup (f45707d) resolves the problems you encountered during unit tests for matplotlib-dependent packages? I think it should do, but it would be good to make sure.

Copy link
Member

Thanks @jayaddison, this is a bug that I introduced and the fix here looks good to me. I think you are looking in the right place for a test, but it might be a little awkward due to the subprocess approach.

Copy link
Contributor Author

Thank you @ianthomas23 @timhoffm for the code reviews. Also, I think I've figured out what could be some viable test coverage:

Given that test_backend_fallback_headless tests a negative/failure case (the ability to open a tkagg output when headless mode is active is expected to cause a failure), I think that test coverage for this change should add a second headless test that presents a successful rendering case. And to do that, it should be possible I think to import matplotlib.pyplot followed by creating a plot directly (no matplotlib.use / other imports required).

I've tested that theory in a shell with and without the fix in place and it seems to behave as expected; I'll try adding it to the test suite in a few moments.

This comment was marked as outdated.

This comment was marked as resolved.

@jayaddison jayaddison marked this pull request as draft March 11, 2025 13:08

This comment was marked as outdated.

This comment was marked as outdated.

This comment was marked as resolved.

@jayaddison jayaddison marked this pull request as ready for review March 12, 2025 19:35
@tacaswell tacaswell added this to the v3.10.2 milestone Mar 12, 2025
Copy link
Member

Can we squash this to one or two commits?

jayaddison reacted with thumbs up emoji

Co-authored-by: Ian Thomas <ianthomas23@gmail.com>
@jayaddison jayaddison force-pushed the issue-29713/auto-backend-case-sensitivity-fixup branch from b07d9c6 to 2ed08c1 Compare March 12, 2025 21:51
Copy link

nileshpatra commented Mar 13, 2025 via email

Now that all comments are addressed, could this be merged?
On 13 March 2025 2:18:25 am IST, Thomas A Caswell ***@***.***> wrote: tacaswell left a comment (matplotlib/matplotlib#29721) Can we squash this to one or two commits? -- Reply to this email directly or view it on GitHub: #29721 (comment) You are receiving this because you were mentioned. Message ID: ***@***.***>
Best, Nilesh

Copy link
Member

@ianthomas23 ianthomas23 left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks @jayaddison. CI failure is unrelated.

@ianthomas23 ianthomas23 merged commit 309d380 into matplotlib:main Mar 14, 2025
36 of 38 checks passed
Copy link
Contributor Author

Thank you @ianthomas23!

@jayaddison jayaddison deleted the issue-29713/auto-backend-case-sensitivity-fixup branch March 14, 2025 09:31
dstansby added a commit that referenced this pull request Mar 21, 2025
...721-on-v3.10.x
Backport PR #29721 on branch v3.10.x (FIX: pyplot auto-backend detection case-sensitivity fixup)
@ksunden ksunden mentioned this pull request May 9, 2025
5 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Reviewers

@ianthomas23 ianthomas23 ianthomas23 approved these changes

@timhoffm timhoffm timhoffm approved these changes

Assignees
No one assigned
Projects
None yet
Milestone
v3.10.3
Development

Successfully merging this pull request may close these issues.

[Bug]: Matplotlib selects TkAgg backend on LXC containers

AltStyle によって変換されたページ (->オリジナル) /