【问题标题】:Throws UnicodeEncodeError when python script run via cron当 python 脚本通过 cron 运行时抛出 UnicodeEncodeError
【发布时间】:2019-12-24 06:54:29
【问题描述】:

我的 python 脚本在手动运行时运行良好,但无论何时从 cron 作业运行它都会抛出 UnicodeEncodeError: 'ascii' codec can't encode character '\u2122' in position 0: ordinal not in range(128)

我的文件.py:

print(u"\u2122")

我在 macos high sierra 而python版本是3.6.0

【问题讨论】:

  • 您可以粘贴您的代码吗?这将帮助人们了解导致问题的原因。
  • @mishsx 已粘贴。代码是简单的打印
  • 刚刚通过将LC_ALL="en_US.UTF-8" 放在我的 crontab 列表顶部使其工作。这是一种 hack,在其他环境中它可以在没有这个的情况下工作。
  • 这让我印象深刻,因为我之前也修复了 PATH 变量这样的问题。理想情况下,在 crontab 文件中应该只有作业。因此,必须重建或重新安装操作系统级别的某些东西才能使修复正确
  • 你应该研究为什么它在其他环境中也能工作,但仅限于此。是否有一些设置会导致这种情况?迟早有人可能会在明天面临同样的问题,因此请确保放置 cmets 以帮助下次看到此内容的人。

标签: python-3.x cron python-unicode cron-task


【解决方案1】:

通过放置在 crontab 顶部来修复它:

LC_ALL="en_US.UTF-8"

【讨论】:

    猜你喜欢
    • 2011-03-27
    • 2022-09-30
    • 1970-01-01
    • 2019-07-23
    • 2021-11-21
    • 1970-01-01
    • 2015-03-16
    • 2017-08-11
    • 2016-10-01
    相关资源
    最近更新 更多