Skip to content

matplotlib.use('agg', force=True) does not ignore unavailable configured backend #17763

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

Closed
QuLogic opened this issue Jun 26, 2020 · 0 comments · Fixed by #17764
Closed

matplotlib.use('agg', force=True) does not ignore unavailable configured backend #17763

QuLogic opened this issue Jun 26, 2020 · 0 comments · Fixed by #17764
Milestone

Comments

@QuLogic
Copy link
Member

QuLogic commented Jun 26, 2020

Bug report

Bug summary

Tests are supposed to run with Agg backend, but if you have a different backend configured, and your environment does not have its dependencies available, the tests try to load it instead.

As matplotlib.use('agg', force=True) is called first, this implies it should be ignoring failure to load the configured backend, since it's not Agg

Code for reproduction

$ conda create -n test python=3.6 numpy  # etc., but not Qt
$ conda activate test
$ pip install -e .
$ vim ~/.config/matplotlib/matplotlibrc  # Set backend to Qt5Agg
$ pytest -n4

Actual outcome

___________________________________________________________ ERROR collecting test session ____________________________________________________________
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/_pytest/config/__init__.py:495: in _importconftest
    return self._conftestpath2mod[key]
E   KeyError: PosixPath('/home/elliott/code/matplotlib/lib/matplotlib/tests/conftest.py')

During handling of the above exception, another exception occurred:
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/py/_path/common.py:383: in visit
    for x in Visitor(fil, rec, ignore, bf, sort).gen(self):
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/py/_path/common.py:435: in gen
    for p in self.gen(subdir):
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/py/_path/common.py:424: in gen
    dirs = self.optsort([p for p in entries
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/py/_path/common.py:425: in <listcomp>
    if p.check(dir=1) and (rec is None or rec(p))])
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/_pytest/nodes.py:506: in _recurse
    ihook = self._gethookproxy(dirpath)
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/_pytest/nodes.py:487: in _gethookproxy
    my_conftestmodules = pm._getconftestmodules(fspath)
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/_pytest/config/__init__.py:473: in _getconftestmodules
    mod = self._importconftest(conftestpath)
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/_pytest/config/__init__.py:520: in _importconftest
    self.consider_conftest(mod)
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/_pytest/config/__init__.py:575: in consider_conftest
    self.register(conftestmodule, name=conftestmodule.__file__)
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/_pytest/config/__init__.py:379: in register
    ret = super().register(plugin, name)
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/pluggy/manager.py:121: in register
    hook._maybe_apply_history(hookimpl)
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/pluggy/hooks.py:336: in _maybe_apply_history
    res = self._hookexec(self, [method], kwargs)
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/pluggy/manager.py:87: in _hookexec
    return self._inner_hookexec(hook, methods, kwargs)
/var/container/conda/envs/mpl36npm/lib/python3.6/site-packages/pluggy/manager.py:81: in <lambda>
    firstresult=hook.spec.opts.get("firstresult") if hook.spec else False,
lib/matplotlib/testing/conftest.py:23: in pytest_configure
    matplotlib.use('agg', force=True)
lib/matplotlib/__init__.py:1144: in use
    from matplotlib import pyplot as plt
lib/matplotlib/pyplot.py:2327: in <module>
    switch_backend(rcParams["backend"])
lib/matplotlib/pyplot.py:259: in switch_backend
    class backend_mod(matplotlib.backend_bases._Backend):
lib/matplotlib/pyplot.py:260: in backend_mod
    locals().update(vars(importlib.import_module(backend_name)))
/var/container/conda/envs/mpl36npm/lib/python3.6/importlib/__init__.py:126: in import_module
    return _bootstrap._gcd_import(name[level:], package, level)
lib/matplotlib/backends/backend_qt5agg.py:11: in <module>
    from .backend_qt5 import (
lib/matplotlib/backends/backend_qt5.py:16: in <module>
    import matplotlib.backends.qt_editor.figureoptions as figureoptions
lib/matplotlib/backends/qt_editor/figureoptions.py:11: in <module>
    from matplotlib.backends.qt_compat import QtGui
lib/matplotlib/backends/qt_compat.py:173: in <module>
    raise ImportError("Failed to import any qt binding")
E   ImportError: Failed to import any qt binding
============================================================== short test summary info ===============================================================
ERROR  - ImportError: Failed to import any qt binding
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! Interrupted: 1 error during collection !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!

Matplotlib version

  • Operating system: Fedora 31
  • Matplotlib version: master 4a143b9
  • Matplotlib backend (print(matplotlib.get_backend())): Qt5Agg
  • Python version: 3.6.10
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Jun 26, 2020
In matplotlib.use we import pyplot to use `switch_backend`, however
if the user calls  `mpl.use(...)` before importing pyplot then
during the initial import of pyplot, before we set the selected
backend we try to set the backend set via rcParams.

This change only imports pyplot if it is already imported, otherwise
it is safe to just set the rcParams and not go through the full
`plt.switch_backend` path.

closes matplotlib#17763
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Jun 28, 2020
In matplotlib.use we import pyplot to use `switch_backend`, however
if the user calls  `mpl.use(...)` before importing pyplot then
during the initial import of pyplot, before we set the selected
backend we try to set the backend set via rcParams.

This change only imports pyplot if it is already imported, otherwise
it is safe to just set the rcParams and not go through the full
`plt.switch_backend` path.

closes matplotlib#17763
tacaswell added a commit to tacaswell/matplotlib that referenced this issue Jun 28, 2020
In matplotlib.use we import pyplot to use `switch_backend`, however
if the user calls  `mpl.use(...)` before importing pyplot then
during the initial import of pyplot, before we set the selected
backend we try to set the backend set via rcParams.

This change only imports pyplot if it is already imported, otherwise
it is safe to just set the rcParams and not go through the full
`plt.switch_backend` path.

closes matplotlib#17763
@QuLogic QuLogic added this to the v3.3.0 milestone Jun 29, 2020
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 a pull request may close this issue.

1 participant