Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Locale on django and uwsgi UnicodeEncodeError

EDIT: I just realized, that when i'm not trying to print to console that variable, it works. Why?

I run into an issue related to displaying string label with utf chars. I set locale env in uwsgi ini file like this:

env =LC_ALL=en_US.UTF-8
env =LANG=en_US.UTF-8

and in wsgi.py:

locale.setlocale(locale.LC_ALL, 'en_US.UTF-8')

When I run app code:

print (locale.getlocale(), locale.getpreferredencoding())
print locale.getdefaultlocale()
print "option_value", option_value
label = force_text(option_label)
print 'label', label #THIS FAILS

the output is:

(('en_US', 'UTF-8'), 'UTF-8')
('en_US', 'UTF-8')
option_value d
ERROR <stack trace>
print 'label', label
UnicodeEncodeError: 'ascii' codec can't encode character u'\u015b' in position 5: ordinal not in range(128)

The problem is not present when I run app via runserver in production environment. Django 1.6.5 Python 2.7.6 Ubuntu 14.04 uWSGI 2.0.5.1

like image 989
okrutny Avatar asked Jul 22 '14 12:07

okrutny


1 Answers

I just found answer here: http://chase-seibert.github.io/blog/2014/01/12/python-unicode-console-output.html

Realized that the console is responsible for that error, so exporting additional env variable in uwsgi config file solves the issue: env = PYTHONIOENCODING=UTF-8

like image 119
okrutny Avatar answered Oct 23 '22 11:10

okrutny