Python:为什么从 cron 调用时会从 Babel 得到这个异常?

发布于 2024-10-15 01:55:20 字数 1279 浏览 1 评论 0原文

当我的脚本调用 Babel 函数时,我从 Python Babel 包中抛出此异常。问题是当我从命令行运行它时它工作正常。然而,这是我从 cron 运行它时得到的错误。看来这可能与某些环境或区域设置或缺乏它有关。

File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 508, in format_datetime
       return parse_pattern(format).apply(datetime, locale)
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 702, in apply
       return self % DateTimeFormat(datetime, locale)
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 699, in __mod__
       return self.format % other
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 724, in __getitem__
       return self.format_month(char, num)
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 793, in format_month
       return get_month_names(width, context, self.locale)[self.value.month]
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 87, in get_month_names
       return Locale.parse(locale).months[context][width]
   AttributeError: 'NoneType' object has no attribute 'months'

更新

从命令行区域设置调用时为 en_US,从 cron 调用时为 None。从 cron 调用时如何设置?

另外,不知道这是否重要,但是调用 Babel 的脚本是自定义的 django-admin 命令。

I'm getting this exception thrown from within the Python Babel package when my script calls a Babel function. The thing is when I run it from the command line it works okay. However, this is the error I get when I run it from cron. It seems though it may be related to some environment or locale setting or lack of it.

File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 508, in format_datetime
       return parse_pattern(format).apply(datetime, locale)
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 702, in apply
       return self % DateTimeFormat(datetime, locale)
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 699, in __mod__
       return self.format % other
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 724, in __getitem__
       return self.format_month(char, num)
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 793, in format_month
       return get_month_names(width, context, self.locale)[self.value.month]
     File "/home/myproj/lib/python2.6/Babel-0.9.5-py2.6.egg/babel/dates.py", line 87, in get_month_names
       return Locale.parse(locale).months[context][width]
   AttributeError: 'NoneType' object has no attribute 'months'

UPDATE

When called from the command line locale is en_US and when called from cron None. How do I set it when calling it from cron?

Also, don't know if this is important, but the script that calls into Babel is a custom django-admin command.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

夕嗳→ 2024-10-22 01:55:20

Babel 无法检测默认区域设置(对于 LC_TIME),因为 cron 设置的环境变量较少。

您可以明确设置babel.dates.LC_TIME = Locale.parse('en_US')。另请查看 Babel 0.9.6 - 我们在默认区域设置检测中进行了一些修复。

Babel was unable to detect a default locale (for LC_TIME) because cron sets fewer environment variables.

You could set this explicitely babel.dates.LC_TIME = Locale.parse('en_US'). Also check out Babel 0.9.6 - there we some fixes in default locale detection.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文