matplotlib RuntimeError in cron job before actual import
Question: I have a module I'm writing that uses matplotlib. However, I need it to work
with a display(e.g. command-line execution), or when theres no display (
SGE/qsub cluster job, or a cron job).

I found this answer ( [Automatic detection of display availability with
matplotlib](http://stackoverflow.com/questions/8257385/automatic-detection-of-
display-availability-with-matplotlib) ), which works for the command-line
execution and within a cluster job. But, in a cron job, it fails to properly
import matplotlib, and seems to throw an exception before I import it.

In the cronjob, I try to import my module with the following script:

    
    
    import os    
    os.environ['PYTHONPATH'] = 'path/to/my/module/dir:%s' % os.environ['PYTHONPATH']
    
    print 'now loading mymodule...'
    import mymodule
    

and mymodule.py just has the code from the earlier linked answer:

    
    
    #!/usr/bin/python
    import os
    
    print 'testing import method...'
    import matplotlib
    r = 0
    try :
      r = os.system('python -c "import matplotlib.pyplot as plt;plt.figure()"')
    except RuntimeError :
      pass
    print 'r=%d' % r
    if r != 0:
      print 'matplotlib: running in cluster, using Agg!'
      matplotlib.use('Agg')
    import matplotlib.pyplot as plt
    

I get this error message from the cron service:

    
    
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "/afs/ifh.de/user/u/user/.local/lib/python2.6/site-packages/matplotlib/pyplot.py", line 109, in <module>
        _backend_mod, new_figure_manager, draw_if_interactive, _show = pylab_setup()
      File "/afs/ifh.de/user/u/user/.local/lib/python2.6/site-packages/matplotlib/backends/__init__.py", line 32, in pylab_setup
        globals(),locals(),[backend_name],0)
      File "/afs/ifh.de/user/u/user/.local/lib/python2.6/site-packages/matplotlib/backends/backend_gtkagg.py", line 14, in <module>
        from matplotlib.backends.backend_gtk import gtk, FigureManagerGTK, FigureCanvasGTK,\
      File "/afs/ifh.de/user/u/user/.local/lib/python2.6/site-packages/matplotlib/backends/backend_gtk.py", line 16, in <module>
        import gtk; gdk = gtk.gdk
      File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 64, in <module>
        _init()
      File "/usr/lib64/python2.6/site-packages/gtk-2.0/gtk/__init__.py", line 52, in _init
        _gtk.init_check()
    RuntimeError: could not open display
    now loading mymodule...
    testing import method...
    r=256
    matplotlib: running in cluster, using Agg!
    

I think its trying and failing to import matplotlib, which is throwing a
RuntimeError, _before_ getting to my code in mymodule.py . I can see that it
gets to my code and runs it, but the Traceback message makes me nervous.

Why is it loading matplotlib(and crashing) before I tell it to(with Agg)? How
can I fix/hide that traceback message?


Answer: As a dirty trick to hide the traceback message, just pipe all errors in the
cronjob to `/dev/null`:

    
    
    script.py 2> /dev/null
    

