Python:为什么从 cron 调用时会从 Babel 得到这个异常?
当我的脚本调用 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
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.