Skip to content

Commit a7e5f6f

Browse files
authored
Merge pull request #7303 from anntzer/setting-backends-twice-traceback
[MRG+1] Traceback to help fixing double-calls to mpl.use.
2 parents 96796d8 + 8ce25d1 commit a7e5f6f

File tree

2 files changed

+17
-4
lines changed

2 files changed

+17
-4
lines changed

lib/matplotlib/__init__.py

+12-4
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

+5
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)