Skip to content

Traceback to help fixing double-calls to mpl.use. #7303

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
Oct 25, 2016

Conversation

anntzer
Copy link
Contributor

@anntzer anntzer commented Oct 18, 2016

Example session:

$ python -c 'import matplotlib as mpl; mpl.use("agg"); import matplotlib.pyplot; mpl.use("qt5agg")'
-c:1: UserWarning:
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was originally set to 'agg' by the following code:
File "<string>", line 1, in <module>
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/antony/src/extern/matplotlib/lib/matplotlib/pyplot.py", line 68, in <module>
    from matplotlib.backends import pylab_setup
File "<frozen importlib._bootstrap>", line 969, in _find_and_load
File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
File "<frozen importlib._bootstrap_external>", line 665, in exec_module
File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
File "/home/antony/src/extern/matplotlib/lib/matplotlib/backends/__init__.py", line 13, in <module>
    _backend_loading_tb = "".join(traceback.format_stack())

(In practice, you'd expect the two calls to be further apart from each
other, of course.)

Note that the current traceback can be accessed by standard means,
e.g. by setting the warnings filter.

Not sure how easy this is to test.

See #7287.

@tacaswell tacaswell added this to the 2.1 (next point release) milestone Oct 19, 2016
@tacaswell
Copy link
Member

The doc failure looks un-related and due to merge conflicts.

@tacaswell tacaswell changed the title Traceback to help fixing double-calls to mpl.use. [MRG+1] Traceback to help fixing double-calls to mpl.use. Oct 19, 2016
@Kojoley
Copy link
Member

Kojoley commented Oct 19, 2016

The message looks weird now. warnings.warn prints the line of code that triggered the warning after the actual message, so we have this:

>python test_use.py
test_use.py:7: UserWarning:
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to 'TkAgg' by the following code:
  File "test_use.py", line 4, in <module>
    import matplotlib.backends
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "...\lib\matplotlib\backends\__init__.py", line 13, in <module>
    _backend_loading_tb = "".join(traceback.format_stack())


  matplotlib.use('template')

@anntzer
Copy link
Contributor Author

anntzer commented Oct 19, 2016

Just to clarify, another example:
a.py

def set_to_qt4agg():
    import matplotlib
    matplotlib.use("qt4agg")
    import matplotlib.pyplot

set_to_qt4agg()
import b; b.set_to_qt5agg()

b.py

def set_to_qt5agg():
    import matplotlib
    matplotlib.use("qt5agg")
    import matplotlib.pyplot

python a.py gives

/tmp/b.py:3: UserWarning: 
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to 'Qt4Agg' by the following code:
  File "a.py", line 6, in <module>
    set_to_qt4agg()
  File "a.py", line 4, in set_to_qt4agg
    import matplotlib.pyplot
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/antony/src/extern/matplotlib/lib/matplotlib/pyplot.py", line 68, in <module>
    from matplotlib.backends import pylab_setup
  File "<frozen importlib._bootstrap>", line 969, in _find_and_load
  File "<frozen importlib._bootstrap>", line 958, in _find_and_load_unlocked
  File "<frozen importlib._bootstrap>", line 673, in _load_unlocked
  File "<frozen importlib._bootstrap_external>", line 665, in exec_module
  File "<frozen importlib._bootstrap>", line 222, in _call_with_frames_removed
  File "/home/antony/src/extern/matplotlib/lib/matplotlib/backends/__init__.py", line 13, in <module>
    _backend_loading_tb = "".join(traceback.format_stack())


  matplotlib.use("qt5agg")

@efiring
Copy link
Member

efiring commented Oct 22, 2016

I think this message will leave most users baffled. Filtering out all the importlib stuff might help.

@anntzer anntzer force-pushed the setting-backends-twice-traceback branch from aab3ab3 to f119e5f Compare October 23, 2016 04:44
@anntzer
Copy link
Contributor Author

anntzer commented Oct 23, 2016

Filtered out importlib line noise manually (possibly relevant python.org issues: http://bugs.python.org/issue15110, http://bugs.python.org/issue15386, http://bugs.python.org/issue15425).

Now:

$ python -c 'import matplotlib as mpl; mpl.use("agg"); import matplotlib.pyplot; mpl.use("qt5agg")'
-c:1: UserWarning: 
This call to matplotlib.use() has no effect because the backend has already
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
or matplotlib.backends is imported for the first time.

The backend was *originally* set to 'agg' by the following code:
  File "<string>", line 1, in <module>
  File "/home/antony/src/extern/matplotlib/lib/matplotlib/pyplot.py", line 68, in <module>
    from matplotlib.backends import pylab_setup
  File "/home/antony/src/extern/matplotlib/lib/matplotlib/backends/__init__.py", line 14, in <module>
    line for line in traceback.format_stack()

We may also want to strip out the innermost frame but that may do more harm than good...

@QuLogic
Copy link
Member

QuLogic commented Oct 23, 2016

Might want to update the commit message too, but it's not super important if nothing else requires any changes.

Example session:

    $ python -c 'import matplotlib as mpl; mpl.use("agg"); import matplotlib.pyplot; mpl.use("qt5agg")'
    -c:1: UserWarning:
    This call to matplotlib.use() has no effect because the backend has already
    been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
    or matplotlib.backends is imported for the first time.

    The backend was *originally* set to 'agg' by the following code:
    File "<string>", line 1, in <module>
    File "/home/antony/src/extern/matplotlib/lib/matplotlib/pyplot.py", line 68, in <module>
        from matplotlib.backends import pylab_setup
    File "/home/antony/src/extern/matplotlib/lib/matplotlib/backends/__init__.py", line 14, in <module>
        line for line in traceback.format_stack()

(In practice, you'd expect the two calls to be further apart from each
other, of course.)

Note that the *current* traceback can be accessed by standard means,
e.g. by setting the warnings filter.

Not sure how easy this is to test.
@anntzer anntzer force-pushed the setting-backends-twice-traceback branch from f119e5f to 8ce25d1 Compare October 23, 2016 04:54
@anntzer
Copy link
Contributor Author

anntzer commented Oct 23, 2016

Good catch, fixed.

@NelleV
Copy link
Member

NelleV commented Oct 25, 2016

Thanks @anntzer !

@NelleV NelleV merged commit a7e5f6f into matplotlib:master Oct 25, 2016
@anntzer anntzer deleted the setting-backends-twice-traceback branch October 25, 2016 16:02
@QuLogic QuLogic changed the title [MRG+1] Traceback to help fixing double-calls to mpl.use. Traceback to help fixing double-calls to mpl.use. Oct 25, 2016
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.

7 participants