【问题标题】:Batch job fails when using matplotlib [duplicate]使用 matplotlib 时批处理作业失败 [重复]
【发布时间】:2018-06-22 23:58:34
【问题描述】:

我尝试在我的 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&gt;&amp;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 中手动执行脚本(如顶部所述),它可以工作。

【问题讨论】:

  • batch使用的工作目录是什么?如果您使用绝对文件名(例如 plt.savefig("/tmp/test.png")tee /tmp/output.txt 会怎样?
  • @match 我尝试使用绝对路径...但仍然只创建了一个空的output.txt
  • 接下来要测试的是一个简单的作业是否成功运行。例如将echo "Hello" | tee output.txt 放入batch_job.txt 如果这样可行,那么我会假设python 脚本由于某种原因而崩溃——在这种情况下,将batch_job.txt 更改为包含python plot.py 2&gt;&amp;1 | tee output.txt 以捕获stderr 和stdout。
  • @match 感谢您提供如何捕获标准错误的提示!最后,我得到一个错误。我将它粘贴到 EDIT2 中。

标签: python matplotlib batch-processing


【解决方案1】:

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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-12-12
    • 2021-07-25
    • 2018-11-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多