PyICU在Django shell中工作,但在Web服务器上不工作

时间:2014-01-10 13:33:37

标签: python django icu pyicu

尝试在登台服务器上访问ICU的Collat​​or属性时出现此错误。

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)]

我已经通过Ondrej's PHP5 PPA以及PyICU在本地和我们的登台服务器上成功安装了ICU(libicu52, libicu-dev)。运行Ubuntu Server 13.10的服务器,包含Python 2.7.5,Django 1.5.5,Nginx 1.4.1和uWSGI 1.9.13。

这是完整的追溯:

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返回其实际属性,包括Collat​​or。

ICUError=<class 'icu.ICUError'>,
__builtins__={'bytearray': <type 'bytearray'>,
'IndexError': <type 'exceptions.IndexError'>
[...]

代码在Django的shell中运行的原因可能是什么,但不是通过Web服务器?

1 个答案:

答案 0 :(得分:3)

结果是uWSGI中的配置错误,与PyICU或ICU无关。我发现uWSGI没有使用virtualenv中安装的软件包,所以我再次查看了uWSGI配置文件。

chdir未设置为正确的文件夹,而是指向其父文件夹。

为什么所有其他包都有效,我不知道。