title: UnicodeEncodeError when running in mod_wsgi even after setting LANG and LC_ALL
body: <p>I get a <code>UnicodeEncodeError</code> error when printing a unicode string from my app. It runs via Elastic Beanstalk on AWS (Apache + mod_wsgi). I found <a href="http://blog.dscpl.com.au/2014/09/setting-lang-and-lcall-when-using.html" rel="noreferrer" title="this">this</a> useful, and when I call <code>locale.getdefaultlocale()</code> and <code>locale.getpreferredencoding()</code> I get <code>None</code> and <code>ASCII</code>.</p>
  
  <p>I set <code>LANG</code> and <code>LC_ALL</code> to <code>en_US.UTF-8</code> (via WSGIDaemonProcess directive AND environment variables). Now when I call <code>locale.getdefaultlocale()</code> and <code>locale.getpreferredencoding()</code>, I get <code>('en_US', 'UTF-8')</code> and <code>UTF-8</code>. However, I'm still getting the same <code>UnicodeEncodeError</code>.</p>
  
  <p><code>sys.stdout</code> is of type <code>mod_wsgi.Log</code>. I couldn't find any details on how to check/set the encoding of this.</p>
  
  <p>I'm not sure how to continue debugging at this point. How do I fix this error?</p>
  
  <p>This wsgi.conf is the default from Elastic Beanstalk except for where I added <code>lang</code> and <code>locale</code> to the WSGIDaemonProcess directive.</p>
  
  <pre class="lang-none prettyprint-override"><code>LoadModule wsgi_module modules/mod_wsgi.so
  WSGIPythonHome /opt/python/run/baselinenv
  WSGISocketPrefix run/wsgi
  WSGIRestrictEmbedded On
  
  &lt;VirtualHost *:80&gt;
      Alias /static/ /opt/python/current/app/static/
  
      &lt;Directory /opt/python/current/app/static/&gt;
          Order allow,deny
          Allow from all
      &lt;/Directory&gt;
  
      WSGIScriptAlias / /opt/python/current/app/application.py
  
      &lt;Directory /opt/python/current/app/&gt;
          Require all granted
      &lt;/Directory&gt;
  
      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
  &lt;/VirtualHost&gt;
  </code></pre>
  
  <p>Traceback:</p>
  
  <pre><code>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)
  </code></pre>
  
  <p>Here is the code I'm using to exercise it:</p>
  
  <pre><code># -*- coding: utf-8 -*-
  
  ...
  
  @api_app.route('/venues')
  def get_venues():
      test = u"Ián"
      print "TEST =", test
  </code></pre>