3
votes

I am trying to run a python script using cron. The script runs without issue from the command line but has problems with matplotlib when run from cron. The error is below.

Traceback (most recent call last):

File "/home/ubuntu/python/spread.py", line 154, in plot_spread(lat, lon, vals, mean, maxs, mins, stdp, stdm, ens_members)

File "/home/ubuntu/python/spread.py", line 81, in plot_spread plt.fill_between(x, maxs, stdp, color='r', alpha=0.2)

File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 2785, in fill_between ax = gca()

File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 928, in gca return gcf().gca(**kwargs)

File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 578, in gcf return figure()

File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/pyplot.py", line 527, in figure **kwargs)

File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 46, in new_figure_manager return new_figure_manager_given_figure(num, thisFig)

File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 53, in new_figure_manager_given_figure canvas = FigureCanvasQTAgg(figure)

File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4agg.py", line 76, in init FigureCanvasQT.init(self, figure)

File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt4.py", line 68, in init _create_qApp()

File "/usr/lib/python2.7/dist-packages/matplotlib-1.5.0_818.gfd83789-py2.7-linux-x86_64.egg/matplotlib/backends/backend_qt5.py", line 138, in _create_qApp raise RuntimeError('Invalid DISPLAY variable')

RuntimeError: Invalid DISPLAY variable

1
Perfect! solved the issue. Thank youbgame2498

1 Answers

5
votes

I got a similar error when I tried to import matplotlib.pyplot in python using cron. You can force matplotlib to not use any Xwindows backend. Do this:

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

These links might be also helpful:

Generating a PNG with matplotlib when DISPLAY is undefined

Generating matplotlib graphs without a running X server

Hope this is helpful!