Skip to content

Safely import pyplot if a GUI framework is already running. #18316

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

Merged
merged 1 commit into from
Aug 22, 2020

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Aug 21, 2020

configure_subplots now needs to import pyplot (because that's
how we can spin up a new figure as needed), but when relying on
embedding we could well reach that point with an already running
interactive framework, pyplot never imported, and an incompatible
rcParams["backend"] (which had not been read yet). For example, run
examples/user_interfaces/embedding_in_tk_sgskip.py with a non-tk
backend set in your matplotlibrc: clicking on the the configure_subplots
button triggers a "cannot load backend 'foo' which requires the 'foo'
interactive framework, as 'bar' is currently running".

Instead, use a helper to safely import pyplot when another interactive
framework may already be running. The list of frameworks that
_get_running_interactive_framework returns is fixed, so we can just
hardcode the mapping. (We are going to ignore the user-set
rcParams["backend"] anyways.)

followup to #16818, especially #16818 (review) (which this does not fix, it's just the easy first step).

PR Summary

PR Checklist

  • Has Pytest style unit tests
  • Code is Flake 8 compliant
  • New features are documented, with examples if plot related
  • Documentation is sphinx and numpydoc compliant
  • Added an entry to doc/users/next_whats_new/ if major new feature (follow instructions in README.rst there)
  • Documented in doc/api/next_api_changes/* if API changed in a backward-incompatible way

`configure_subplots` now needs to import `pyplot` (because that's
how we can spin up a new figure as needed), but when relying on
embedding we could well reach that point with an already running
interactive framework, pyplot never imported, and an incompatible
rcParams["backend"] (which had not been read yet).  For example, run
`examples/user_interfaces/embedding_in_tk_sgskip.py` with a non-tk
backend set in your matplotlibrc: clicking on the the configure_subplots
button triggers a "cannot load backend 'foo' which requires the 'foo'
interactive framework, as 'bar' is currently running".

Instead, use a helper to safely import pyplot when another interactive
framework may already be running.  The list of frameworks that
`_get_running_interactive_framework` returns is fixed, so we can just
hardcode the mapping.  (We are going to ignore the user-set
`rcParams["backend"]` anyways.)
@anntzer anntzer added this to the v3.4.0 milestone Aug 21, 2020
@tacaswell
Copy link
Member

Does this catch a case not covered by #18245 ?

@anntzer
Copy link
Contributor Author

anntzer commented Aug 21, 2020

This is independent of #18245: here rcParams["backend"] != auto_backend_sentinel, but we still want to fallback because we're forced anyways.

@tacaswell
Copy link
Member

feels a bit like epicycles, but still suspect that this is moving us in the direction of a simplification.

@QuLogic QuLogic merged commit 3d725f6 into matplotlib:master Aug 22, 2020
@anntzer anntzer deleted the safe_pyplot_import branch November 16, 2021 14:52
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants