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

Appearance settings

Commit 8ce25d1

Browse filesBrowse files
committed
Traceback to help fixing double-calls to mpl.use.
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.
1 parent acf2953 commit 8ce25d1
Copy full SHA for 8ce25d1

File tree

Expand file treeCollapse file tree

2 files changed

+17
-4
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+17
-4
lines changed

‎lib/matplotlib/__init__.py

Copy file name to clipboardExpand all lines: lib/matplotlib/__init__.py
+12-4Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1341,10 +1341,13 @@ def rc_file_defaults():
13411341
"""
13421342
rcParams.update(rcParamsOrig)
13431343

1344-
_use_error_msg = """ This call to matplotlib.use() has no effect
1345-
because the backend has already been chosen;
1346-
matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
1344+
_use_error_msg = """
1345+
This call to matplotlib.use() has no effect because the backend has already
1346+
been chosen; matplotlib.use() must be called *before* pylab, matplotlib.pyplot,
13471347
or matplotlib.backends is imported for the first time.
1348+
1349+
The backend was *originally* set to {backend!r} by the following code:
1350+
{tb}
13481351
"""
13491352

13501353

@@ -1385,7 +1388,12 @@ def use(arg, warn=True, force=False):
13851388
if 'matplotlib.backends' in sys.modules:
13861389
# Warn only if called with a different name
13871390
if (rcParams['backend'] != name) and warn:
1388-
warnings.warn(_use_error_msg, stacklevel=2)
1391+
import matplotlib.backends
1392+
warnings.warn(
1393+
_use_error_msg.format(
1394+
backend=rcParams['backend'],
1395+
tb=matplotlib.backends._backend_loading_tb),
1396+
stacklevel=2)
13891397

13901398
# Unless we've been told to force it, just return
13911399
if not force:

‎lib/matplotlib/backends/__init__.py

Copy file name to clipboardExpand all lines: lib/matplotlib/backends/__init__.py
+5Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,15 @@
55

66
import matplotlib
77
import inspect
8+
import traceback
89
import warnings
910

1011

1112
backend = matplotlib.get_backend()
13+
_backend_loading_tb = "".join(
14+
line for line in traceback.format_stack()
15+
# Filter out line noise from importlib line.
16+
if not line.startswith(' File "<frozen importlib._bootstrap'))
1217

1318

1419
def pylab_setup(name=None):

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.