RankoR RankoR - 6 months ago 69
Python Question

Django uses ANSI_X3.4-1968 encoding instead of UTF-8

I'm running Ubuntu 14.04 server with Django + Gunicorn + Nginx.

Nginx config contains line for utf-8 charset.

Locale output for user "auction" (this user runs the server):

LANG=en_US.UTF-8
LANGUAGE=
LC_CTYPE="en_US.utf8"
LC_NUMERIC="en_US.utf8"
LC_TIME="en_US.utf8"
LC_COLLATE="en_US.utf8"
LC_MONETARY="en_US.utf8"
LC_MESSAGES="en_US.utf8"
LC_PAPER="en_US.utf8"
LC_NAME="en_US.utf8"
LC_ADDRESS="en_US.utf8"
LC_TELEPHONE="en_US.utf8"
LC_MEASUREMENT="en_US.utf8"
LC_IDENTIFICATION="en_US.utf8"
LC_ALL=en_US.utf8


Filesystem encoding for this user:

sys.getfilesystemencoding()
'UTF-8'


But when I'm calling the same method in the view, it returns 'ANSI_X3.4-1968'.

So, I can not upload the files with non-latin filenames.

I've spent few days googling the solutions, nothing helps.

How to fix it?

Answer

This sounds like you are running into bug # 13643.

The easiest way to resolve this issue is to set the environment variable PYTHONIOENCODING to utf-8.

If you are using a virtual environment, make sure you set it up explicitly in the virtual environment; as it may not read the global variables.