gpt4 book ai didi

python - 即使在设置 LANG 和 LC_ALL 后,在 mod_wsgi 中运行时也会出现 UnicodeEncodeError

转载 作者:行者123 更新时间:2023-12-03 09:40:13 27 4
gpt4 key购买 nike

我得到一个 UnicodeEncodeError从我的应用程序打印 unicode 字符串时出错。它通过 AWS 上的 Elastic Beanstalk (Apache + mod_wsgi) 运行。我找到了 this很有用,当我打电话时 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 的默认设置,除了我添加的地方 langlocale到 WSGIDaemonProcess 指令。

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>

追溯:
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)

这是我用来练习它的代码:
# -*- coding: utf-8 -*-

...

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

最佳答案

我遵循了@kchomski 的建议并编辑了 /etc/apache2/envvars :

## Uncomment the following line to use the system default locale instead:
. /etc/default/locale

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

关于python - 即使在设置 LANG 和 LC_ALL 后,在 mod_wsgi 中运行时也会出现 UnicodeEncodeError,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30634777/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com