Python 从 crontab 调用时脚本崩溃

Python script crashes when called from crontab

我有一个脚本(托管在 DigitalOcean ubuntu 18.04.3 droplet 上)从 crontab 执行,看起来像这样

0 5 * * * cd /home/john/clips/; /home/john/clips/venv/bin/python3 /home/john/clips/clip_compilator.py

它可以工作,但是一旦它到达其中一个脚本中的下一行

name = mp.TextClip(f"John".upper(), 
    color='#6441A4', 
    stroke_color="black", 
    align='West',
    fontsize=90, 
    font='BigNoodleTitling', method='label')\
                        .margin(left=95, opacity=0)\
                        .set_position(("left", "top"))

它崩溃了

Traceback (most recent call last):
  File "/home/john/clips/venv/lib/python3.6/site-packages/moviepy/video/VideoClip.py", line 1161, in __init__
    subprocess_call(cmd, logger=None)
  File "/home/john/clips/venv/lib/python3.6/site-packages/moviepy/tools.py", line 46, in subprocess_call
    proc = sp.Popen(cmd, **popen_params)
  File "/usr/lib/python3.6/subprocess.py", line 729, in __init__
    restore_signals, start_new_session)
  File "/usr/lib/python3.6/subprocess.py", line 1364, in _execute_child
    raise child_exception_type(errno_num, err_msg, err_filename)
FileNotFoundError: [Errno 2] No such file or directory: 'unset': 'unset'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/john/clips/clip_compilator.py", line 44, in <module>
    current_clip.add_videofile(add_text(current_clip))
  File "/home/john/clips/clip_editor.py", line 16, in add_text
    fontsize=90, font='BigNoodleTitling', method='label')\
  File "/home/john/clips/venv/lib/python3.6/site-packages/moviepy/video/VideoClip.py", line 1170, in __init__
    raise IOError(error)
OSError: MoviePy Error: creation of None failed because of the following error:

[Errno 2] No such file or directory: 'unset': 'unset'.

.This error can be due to the fact that ImageMagick is not installed on your computer, or (for Windows users) that you didn't specify the path to the ImageMagick binary in file conf.py, or that the path you specified is incorrect

当我 运行 在 bash(crontab 之外)中执行相同的命令时,一切正常。
由于某种原因,它特别在 TextClip 上崩溃,我不知道为什么。
有些人建议解决方案可能是在 ImageMagick 的 policy.xml 中注释掉特定行,但我的 convert -list policy 输出是这样的:

通过添加

解决了问题
from moviepy.config import change_settings
change_settings({"IMAGEMAGICK_BINARY": "/usr/local/bin/convert"})

clip_editor.py 的顶部,根据 tomasdms's 解决方案

有导致错误的行