I get a UnicodeEncodeError
error when printing a unicode string from my app. It runs via Elastic Beanstalk on AWS (Apache + mod_wsgi). I found this useful, and when I call locale.getdefaultlocale()
and locale.getpreferredencoding()
I get None
and ASCII
.
I set LANG
and LC_ALL
to en_US.UTF-8
(via WSGIDaemonProcess directive AND environment variables). Now when I call locale.getdefaultlocale()
and locale.getpreferredencoding()
, I get ('en_US', 'UTF-8')
and UTF-8
. However, I'm still getting the same UnicodeEncodeError
.
sys.stdout
is of type mod_wsgi.Log
. I couldn't find any details on how to check/set the encoding of this.
I'm not sure how to continue debugging at this point. How do I fix this error?
This wsgi.conf is the default from Elastic Beanstalk except for where I added lang
and locale
to the WSGIDaemonProcess directive.
LoadModule wsgi_module modules/mod_wsgi.so
WSGIPythonHome /opt/python/run/baselinenv
WSGISocketPrefix run/wsgi
WSGIRestrictEmbedded On
<VirtualHost *:80>
Alias /static/ /opt/python/current/app/static/
<Directory /opt/python/current/app/static/>
Order allow,deny
Allow from all
</Directory>
WSGIScriptAlias / /opt/python/current/app/application.py
<Directory /opt/python/current/app/>
Require all granted
</Directory>
WSGIDaemonProcess wsgi processes=1 threads=15 lang='en_US.UTF-8' locale='en_US.UTF-8' display-name=%{GROUP} python-path=/opt/python/current/app:/opt/python/run/venv/lib/python2.7/site-packages user=wsgi group=wsgi home=/opt/python/current/app
WSGIProcessGroup wsgi
</VirtualHost>
Traceback:
ERROR:discotech:Exception on /venues [GET]
Traceback (most recent call last):
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1817, in wsgi_app
response = self.full_dispatch_request()
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1477, in full_dispatch_request
rv = self.handle_user_exception(e)
File "/opt/python/run/venv/lib/python2.7/site-packages/flask_cors/extension.py", line 110, in wrapped_function
return cors_after_request(app.make_response(f(*args, **kwargs)))
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1381, in handle_user_exception
reraise(exc_type, exc_value, tb)
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1475, in full_dispatch_request
rv = self.dispatch_request()
File "/opt/python/run/venv/lib/python2.7/site-packages/flask/app.py", line 1461, in dispatch_request
return self.view_functions[rule.endpoint](**req.view_args)
File "/opt/python/current/app/discotech/api/venue.py", line 32, in get_venues
print "TEST = %s", test
UnicodeEncodeError: 'ascii' codec can't encode character u'\\xe1' in position 1: ordinal not in range(128)
Here is the code I'm using to exercise it:
# -*- coding: utf-8 -*-
...
@api_app.route('/venues')
def get_venues():
test = u"Ián"
print "TEST =", test
I followed advice by @kchomski and edited /etc/apache2/envvars
:
## Uncomment the following line to use the system default locale instead:
. /etc/default/locale
The last line was commented, I uncommented it. Now my WSGI scripts are using en_US.UTF-8
locale and everything works fine!
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With