UnicodeEncodeError when working in mod_wsgi even after setting LANG and LC_ALL

I am getting an error UnicodeEncodeErrorwhen printing a Unicode string from my application. It passes through an elastic bean loop on AWS (Apache + mod_wsgi). I found this helpful, and when I call locale.getdefaultlocale()and locale.getpreferredencoding(), I get Noneand ASCII.

I installed LANGboth LC_ALLin en_US.UTF-8(via the WSGIDaemonProcess variable and environment variables). Now when I call locale.getdefaultlocale()and locale.getpreferredencoding(), I get ('en_US', 'UTF-8')and UTF-8. However, I am still getting the same UnicodeEncodeError.

sys.stdouthas type mod_wsgi.Log. I could not find any details on how to check / set the encoding of this.

I am not sure how to continue debugging at this point. How to fix this error?

This wsgi.conf is the default value from Elastic Beanstalk, except when I added LANGit localeto 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 use to use it:

# -*- coding: utf-8 -*-

...

@api_app.route('/venues')
def get_venues():
    test = u"Ián"
    print "TEST =", test
+4
source share

All Articles