Cannot handle ImportError call matplotlib.use

I am trying to write plotting code using matplotlib, which works with interactive backends when possible, and non-interactive backends when I work over SSH. I know it is possible to change the backend using matplotlib.use('Agg')

, and I tried using python exception handling to automatically select the correct backend, but I cannot catch the ImportError correctly.

This is what I tried:

try:
    matplotlib.use('MacOSX')
except ImportError:
    matplotlib.use('Agg')

      

and I still get ImportError:

(...)

from matplotlib.backends import _macosx
ImportError: cannot import name _macosx

      

Any idea why except ImportError

not catching an error?

+3


source to share





All Articles