我有一个有效的python3脚本,我已经通过crontab -e设置了crontab。我添加了这一行
* * * * * /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 /Users/harvey/PycharmProjects/3_5_pgm/3woerter.py
cron正在执行脚本,但我收到一封包含此错误消息的电子邮件:
X-Cron-Env: <SHELL=/bin/sh>
X-Cron-Env: <PATH=/usr/bin:/bin>
X-Cron-Env: <LOGNAME=harvey>
X-Cron-Env: <USER=harvey>
X-Cron-Env: <HOME=/Users/harvey>
Message-Id: <20161221161003.1BE13ABFDD0D@harvey-Mac-Pro-4.local>
Date: Wed, 21 Dec 2016 17:10:03 +0100 (CET)
Traceback (most recent call last):
File "/Users/harvey/PycharmProjects/3_5_pgm/3woerter.py", line 43, in <module>
website("http://www.krone.at/nachrichten/rss.html")
File "/Users/harvey/PycharmProjects/3_5_pgm/3woerter.py", line 25, in website
fobj_out.write(str(titleSoup)) # <--- in Ausgabedatei schreiben
UnicodeEncodeError: 'ascii' codec can't encode character '\xf6' in position 121: ordinal not in range(128)
所以我认为cron使用python2而不是python3 当我通过终端直接尝试命令时,一切正常。
我怎样才能确保cron正在使用python3?
答案 0 :(得分:0)
我也碰到了这个!我通过添加LC_ALL
和LANG
解决了这个问题:
LC_ALL=en_US.UTF-8
LANG=en_US.utf-8
* * * * * /Library/Frameworks/Python.framework/Versions/3.6/bin/python3 /Users/harvey/PycharmProjects/3_5_pgm/3woerter.py
我之前学到了如何阅读http://click.pocoo.org/5/python3/#python-3-surrogate-handling以解决另一个问题