甚至在设置LANG和LC_ALL之后,在mod_wsgi中运行时出现UnicodeEncodeError

Ian*_*Ian 5 python unicode utf-8 flask

我得到一个UnicodeEncodeError打印从我的应用程序Unicode字符串时的错误.它通过AWS上的Elastic Beanstalk运行(Apache + mod_wsgi).我发现非常有用,当我打电话locale.getdefaultlocale()locale.getpreferredencoding()我得到的NoneASCII.

我设置LANGLC_ALLen_US.UTF-8(通过WSGIDaemonProcess指令和环境变量).现在,当我打电话locale.getdefaultlocale()locale.getpreferredencoding()我得到('en_US', 'UTF-8')UTF-8.但是,我仍然是一样的UnicodeEncodeError.

sys.stdout是类型的mod_wsgi.Log.我找不到有关如何检查/设置此编码的任何细节.

我不知道如何继续调试.我该如何解决这个错误?

这个wsgi.conf是Elastic Beanstalk的默认设置,除了我添加的地方langlocaleWSGIDaemonProcess指令.

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>
Run Code Online (Sandbox Code Playgroud)

追溯:

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)
Run Code Online (Sandbox Code Playgroud)

这是我用来练习它的代码:

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

...

@api_app.route('/venues')
def get_venues():
    test = u"Ián"
    print "TEST =", test
Run Code Online (Sandbox Code Playgroud)

Ily*_*rov 3

我听从了@kchomski的建议并进行了编辑/etc/apache2/envvars

## Uncomment the following line to use the system default locale instead:
. /etc/default/locale
Run Code Online (Sandbox Code Playgroud)

最后一行已注释,我取消注释。现在我的 WSGI 脚本正在使用en_US.UTF-8语言环境,一切正常!