尝试在登台服务器上访问 ICU 的 Collator 属性时出现此错误。
AttributeError at /...
'module' object has no attribute 'Collator'
当启动 Django 的 shell 并执行完全相同的代码时,一切似乎都和本地一样工作。导致错误的代码是:
import icu
collator = icu.Collator.createInstance(icu.Locale(get_language()))
objects = [i for i in sorted(objects, key=attrgetter('city'), cmp=collator.compare)]
我已经libicu52, libicu-dev
通过Ondrej 的 PHP5 PPA以及 PyICU 在本地和我们的登台服务器上成功安装了 ICU ( )。运行带有 Python 2.7.5、Django 1.5.5、Nginx 1.4.1 和 uWSGI 1.9.13 的 Ubuntu Server 13.10 的服务器。
这是完整的追溯:
File "/var/www/venv/site/local/lib/python2.7/site-packages/django/core/handlers/base.py" in get_response
115. response = callback(request, *callback_args, **callback_kwargs)
File "/var/www/venv/site/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/var/www/venv/site/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
86. return handler(request, *args, **kwargs)
File "/var/www/.../pages/views.py" in get
48. return StoreListView.as_view(object=self.object)(request)
File "/var/www/venv/site/local/lib/python2.7/site-packages/django/views/generic/base.py" in view
68. return self.dispatch(request, *args, **kwargs)
File "/var/www/venv/site/local/lib/python2.7/site-packages/django/views/generic/base.py" in dispatch
86. return handler(request, *args, **kwargs)
File "/var/www/venv/site/local/lib/python2.7/site-packages/django/views/generic/list.py" in get
124. self.object_list = self.get_queryset()
File "/var/www/.../geosearch/views.py" in get_queryset
22. objects = self.sort_objects(objects)
File "/var/www/.../geosearch/views.py" in sort_objects
59. collator = icu.Collator.createInstance(icu.Locale(get_language()))
Exception Type: AttributeError at /...
Exception Value: 'module' object has no attribute 'Collator'
我设法打印了 ICU __dict__
,但它给了我一个 ICUError。在本地,ICU 以其实际属性返回,包括 Collator。
ICUError=<class 'icu.ICUError'>,
__builtins__={'bytearray': <type 'bytearray'>,
'IndexError': <type 'exceptions.IndexError'>
[...]
代码在 Django 的 shell 中正常工作但不能通过 Web 服务器运行的原因可能是什么?