PyICU works in Django shell but not on web server

704 views Asked by At

I'm getting this error when trying to access ICU's Collator attribute on a staging server.

AttributeError at /...
'module' object has no attribute 'Collator'

When launching Django's shell and executing the exact same code, everything seems to be working just as well as it does locally. The code that is causing the error is:

import icu
    collator = icu.Collator.createInstance(icu.Locale(get_language()))
    objects = [i for i in sorted(objects, key=attrgetter('city'), cmp=collator.compare)]

I've successfully installed ICU (libicu52, libicu-dev) via Ondrej's PHP5 PPA, as well as PyICU, both locally and on our staging server. The server running Ubuntu Server 13.10 with Python 2.7.5, Django 1.5.5, Nginx 1.4.1 and uWSGI 1.9.13.

Here's the full traceback:

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'

I managed to print ICU's __dict__, but it gives me an ICUError. Locally, ICU is returned with its actual attributes, including Collator.

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

What might be the reason the code is working as it should in Django's shell, but not through the web server?

1

There are 1 answers

0
JLinden On BEST ANSWER

It turned out to be a misconfiguration in uWSGI, and had nothing to do with PyICU or ICU. I figured out uWSGI was not using the packages installed in the virtualenv, so I had a second look in the uWSGI config file.

chdir was not set to the correct folder, instead it pointed to its parent folder.

Why all the other packages worked, I have no idea.