0

In order for production code to run on machines with different python installations it needs to call

import matplotlib as mpl
mpl.use('Agg')

This is super annoying because it throws the warning

warnings.warn(_use_error_msg) /home/keith/Enthought/Canopy_64bit/User/lib/python2.7/site-packages/matplotlib/init.py:1312: 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.

on some of the machines. I would like to suppress this and only this message. I know I can call

import warnings
warnings.filterwarnings("ignore")

but this might cause me to miss other warnings.

--------------Edit---------------

I have found what may be a solution but I am unsure

import warnings
import matplotlib as mpl
with warnings.catch_warnings():
    warnings.simplefilter("ignore")
    mpl.use('Agg')
Keith
  • 4,646
  • 7
  • 43
  • 72
  • 2
    Why are you unsure? Does it work? – jonrsharpe May 27 '16 at 17:52
  • 1
    Read the warning and fix what it's warning you about! The `mpl.use` call *isn't doing anything*, because of where it is in relation to your other imports. – user2357112 May 27 '16 at 17:53
  • @user2357112 On other machines there is no warning because it does something. jonrsharpe I no longer get the message but am unsure if this is doing other things. In short does this only affect code inside the "with"? – Keith May 27 '16 at 18:15

0 Answers0