【发布时间】:2017-02-07 21:05:49
【问题描述】:
首先我得到了这个 UnicodeError,它在我的本地机器上按原样运行,没有任何错误。
File "/home/malak/www/dev/shopvoice/dashboard/views/payment_view.py", line 126, in paypal_notify
send_login_email_body = SEND_LOGIN_EMAIL_BODY % (user.email, random_pass)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 86: ordinal not in range(128)
当我打印出我得到的 3 个变量的类型时
user.email: <type 'unicode'>
SEND_LOGIN_EMAIL_BODY: <type 'str'>
random_pass: <type 'str'>
第一个问题:
这在 python3 中怎么可能? 当我在本地机器上重建这个错误时,我得到 3 个字符串类型的变量。 python 3中是否存在类似“
显然,首先想到的是,它是否在我的服务器上以 Python 2 运行?
不!
故事中奇怪的部分来了:
这是错误的完整追溯,显然显示了它的python3.6:
[Tue Feb 07 20:39:50.764173 2017] [:error] [pid 21179:tid 140085013579520] Traceback (most recent call last):
[Tue Feb 07 20:39:50.764176 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/django/core/handlers/exception.py", line 39, in inner
[Tue Feb 07 20:39:50.764189 2017] [:error] [pid 21179:tid 140085013579520] response = get_response(request) subinterpreter. Returning 0.
[Tue Feb 07 20:39:50.764192 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 249, in _legacy_get_response
[Tue Feb 07 20:39:50.764195 2017] [:error] [pid 21179:tid 140085013579520] response = self._get_response(request)terpreter. Returning 0.
[Tue Feb 07 20:39:50.764198 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 187, in _get_response
[Tue Feb 07 20:39:50.764201 2017] [:error] [pid 21179:tid 140085013579520] response = self.process_exception_by_middleware(e, request)
[Tue Feb 07 20:39:50.764204 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/django/core/handlers/base.py", line 185, in _get_response
[Tue Feb 07 20:39:50.764207 2017] [:error] [pid 21179:tid 140085013579520] response = wrapped_callback(request, *callback_args, **callback_kwargs)
[Tue Feb 07 20:39:50.764210 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/django/views/decorators/http.py", line 40, in inner
[Tue Feb 07 20:39:50.764213 2017] [:error] [pid 21179:tid 140085013579520] return func(request, *args, **kwargs)
[Tue Feb 07 20:39:50.764216 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58, in wrapped_view
[Tue Feb 07 20:39:50.764219 2017] [:error] [pid 21179:tid 140085013579520] return view_func(*args, **kwargs)
[Tue Feb 07 20:39:50.764222 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/paypal/standard/ipn/views.py", line 99, in ipn
[Tue Feb 07 20:39:50.764224 2017] [:error] [pid 21179:tid 140085013579520] ipn_obj.send_signals()
[Tue Feb 07 20:39:50.764227 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/paypal/standard/ipn/models.py", line 39, in send_signals
[Tue Feb 07 20:39:50.764230 2017] [:error] [pid 21179:tid 140085013579520] valid_ipn_received.send(sender=self)
[Tue Feb 07 20:39:50.764233 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/django/dispatch/dispatcher.py", line 191, in send
[Tue Feb 07 20:39:50.764236 2017] [:error] [pid 21179:tid 140085013579520] response = receiver(signal=self, sender=sender, **named)
[Tue Feb 07 20:39:50.764238 2017] [:error] [pid 21179:tid 140085013579520] File "/home/malak/www/dev/shopvoice/dashboard/views/payment_view.py", line 126, in paypal_notify
[Tue Feb 07 20:39:50.764241 2017] [:error] [pid 21179:tid 140085013579520] send_login_email_body = SEND_LOGIN_EMAIL_BODY % (user.email, random_pass)
[Tue Feb 07 20:39:50.764244 2017] [:error] [pid 21179:tid 140085013579520] UnicodeDecodeError: 'ascii' codec can't decode byte 0xc3 in position 86: ordinal not in range(128)
看看
File "/home/malak/www/dev/shopvoice/venv/lib/python3.6/site-packages/django/views/decorators/csrf.py", line 58,
另一件事是,apache 说它使用 python 2.7.6。 每次我重新启动服务器时,我都会在我的 error.log 中得到这个
[Tue Feb 07 21:34:01.965700 2017] [mpm_event:notice] [pid 21175:tid 140085127694208] AH00489: Apache/2.4.7 (Ubuntu) mod_wsgi/3.4 Python/2.7.6 configured -- resuming normal operations
我在运行 mod_wsgi 的 Ubuntu 14.04 上使用 Apache 2.4.7 运行我的 Django 1.10 应用程序,其中有多个虚拟主机,每个虚拟主机都有自己的 Virtualenv。
第二个问题:如何配置 mod_wsgi 以使用 python3.6,它显然就是这样做的。
【问题讨论】:
-
你需要用你项目中使用的 python 构建 apache 头文件(可能是 virtualenv)
-
你可以用utf-8编码,前段时间我发现了同样的问题......也许你可以用
user.email.encode('utf-8') -
@SancaKembang 我知道如何解决这个错误,问题是这个错误应该不需要处理,因为它的python 3默认使用unicode
-
我以前的项目配置了多个python版本,一次是apache,一次是服务器。项目
A使用了python2.7、B,而其他项目使用了python3.5……我找到了这个答案stackoverflow.com/a/32085001/6396981,“Apache 的 mod_wsgi 模块仅针对一个 Python 版本编译。您无法加载更多同时将一个 mod_wsgi 实例导入 Apache。”最后我重新配置了我所有的项目以兼容相同的python版本...... -
回溯并不表示您正在运行 Python 3,但这些模块位于名为
python3.6的目录中(可能是由 Python 3 创建的virtualenv)。mod_wsgi可能使用 Python 2,但其 Python 路径设置为该目录。你能展示你的mod_wsgi配置吗?
标签: python django apache unicode mod-wsgi