使用matplotlib时,批处理作业失败

时间:2018-01-13 21:49:06

标签: python matplotlib batch-processing

我尝试在我的linux机器上执行以下简化的批处理作业:

root@kali:~/Test_plots# ls
batch_job.txt  plot.py
root@kali:~/Test_plots# more plot.py 
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,4)
y = np.sin(x)
plt.plot(x,y)
plt.savefig("test.png")
print "done"
root@kali:~/Test_plots# more batch_job.txt 
python plot.py | tee output.txt
root@kali:~/Test_plots# batch < batch_job.txt 
warning: commands will be executed using /bin/sh
job 30 at Sat Jan 13 22:35:00 2018
root@kali:~/Test_plots# atq
30  Sat Jan 13 22:35:00 2018 = root
root@kali:~/Test_plots# atq
root@kali:~/Test_plots# ls
batch_job.txt  output.txt  plot.py
root@kali:~/Test_plots# more output.txt
root@kali:~/Test_plots#

作业应执行plot.py并将终端输出保存到output.txt。这项工作似乎失败了,因为output.txt是空的。

如果我手动执行作业,则成功:

root@kali:~/Test_plots# more batch_job.txt 
python plot.py | tee output.txt
root@kali:~/Test_plots# which sh
/bin/sh
root@kali:~/Test_plots# sh
# python plot.py | tee output.txt
Fontconfig warning: ignoring UTF-8: not a valid region tag
done
# exit
root@kali:~/Test_plots# ls
batch_job.txt  output.txt  plot.py  test.png
root@kali:~/Test_plots# more output.txt 
done

情节和output.txt已正确创建。

是否有任何记录我可以检查批处理作业失败的原因?

修改 如果我在脚本中注释掉plt - 行,则批处理作业会正确完成。

root@kali:~/Test_plots# ls
batch_job.txt  plot.py
root@kali:~/Test_plots# more plot.py 
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0,4)
y = np.sin(x)
#plt.plot(x,y)
#plt.savefig("test.png")
print "done"
root@kali:~/Test_plots# batch < batch_job.txt 
warning: commands will be executed using /bin/sh
job 34 at Sun Jan 14 00:02:00 2018
root@kali:~/Test_plots# atq
root@kali:~/Test_plots# ls
batch_job.txt  output.txt  plot.py
root@kali:~/Test_plots# more output.txt 
done
root@kali:~/Test_plots# 

EDIT2 根据@match的评论,我将文件batch_job.txt的内容更改为python plot.py 2>&1 | tee output.txt。现在我收到以下错误:

Fontconfig warning: ignoring UTF-8: not a valid region tag
Traceback (most recent call last):
  File "plot.py", line 5, in <module>
    plt.plot(x,y)
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 3307, in plot
    ax = gca()
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 950, in gca
    return gcf().gca(**kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 586, in gcf
    return figure()
  File "/usr/lib/python2.7/dist-packages/matplotlib/pyplot.py", line 535, in figure
    **kwargs)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 81, in new_figure_manager
    return new_figure_manager_given_figure(num, figure)
  File "/usr/lib/python2.7/dist-packages/matplotlib/backends/backend_tkagg.py", line 89, in new_figure_manager_given_fig
ure
    window = Tk.Tk()
  File "/usr/lib/python2.7/lib-tk/Tkinter.py", line 1822, in __init__
    self.tk = _tkinter.create(screenName, baseName, className, interactive, wantobjects, useTk, sync, use)
_tkinter.TclError: no display name and no $DISPLAY environment variable

为什么DISPLAY变量无效?如果我在批处理作业使用的shell /bin/sh中手动执行脚本(如我在上面所述),它就可以工作。

1 个答案:

答案 0 :(得分:0)

为完整起见,在 EDIT2 中搜索错误成功:_tkinter.TclError: no display name and no $DISPLAY environment variable