最新消息:20210816 当前crifan.com域名已被污染,为防止失联,请关注(页面右下角的)公众号

[已解决]Python代码运行出错:SyntaxError: Non-ASCII character xe5 in file on line 12 but no encoding declared

Python crifan 4891浏览 0评论

折腾:

[已解决]尝试把Python版微信SDK wechat-python-sdk集成到Flask中

期间,在本地Mac中写好了一个UTF8的python文件

放到远程的CentOS中的Flask中,用gunicorn去运行,结果出错:

(SIPEvents) ➜  SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 wechat_auth:app
[2016-08-18 20:43:52 +0000] [22214] [INFO] Starting gunicorn 19.6.0
[2016-08-18 20:43:52 +0000] [22214] [INFO] Listening at: http://127.0.0.1:8080 (22214)
[2016-08-18 20:43:52 +0000] [22214] [INFO] Using worker: sync
[2016-08-18 20:43:52 +0000] [22219] [INFO] Booting worker with pid: 22219
[2016-08-18 20:43:52 +0000] [22219] [ERROR] Exception in worker process
Traceback (most recent call last):
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/arbiter.py”, line 557, in spawn_worker
    worker.init_process()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/base.py”, line 126, in init_process
    self.load_wsgi()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/workers/base.py”, line 136, in load_wsgi
    self.wsgi = self.app.wsgi()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/app/base.py”, line 67, in wsgi
    self.callable = self.load()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py”, line 65, in load
    return self.load_wsgiapp()
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/app/wsgiapp.py”, line 52, in load_wsgiapp
    return util.import_app(self.app_uri)
  File “/root/Envs/SIPEvents/lib/python2.7/site-packages/gunicorn/util.py”, line 357, in import_app
    __import__(module)
  File “/usr/share/nginx/html/SIPEvents/wechat_auth.py”, line 12
SyntaxError: Non-ASCII character ‘\xe5’ in file /usr/share/nginx/html/SIPEvents/wechat_auth.py on line 12, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details
[2016-08-18 20:43:52 +0000] [22219] [INFO] Worker exiting (pid: 22219)
[2016-08-18 20:43:52 +0000] [22214] [INFO] Shutting down: Master
[2016-08-18 20:43:52 +0000] [22214] [INFO] Reason: Worker failed to boot.
(SIPEvents) ➜  SIPEvents

感觉是:

目标环境,不支持,没检测出来当前的Python文件是utf8的

记得好像是要在最开始指定声明当前文件编码的。

UTF8 SyntaxError: Non-ASCII character ‘\xe5’ in file on line 12, but no encoding declared

[已解决]关于python无法显示中文的问题:SyntaxError: Non-ASCII character ‘\xe4’ in file test.py on line 3, but no encoding declared。 – Karry Wang – 博客园

python – SyntaxError: Non-ASCII character ‘\xa3’ in file when function returns ‘£’ – Stack Overflow

python – SyntaxError of Non-ASCII character – Stack Overflow

Python“Non-ASCII character ‘xe5’ in file”报错问题 – henryzhihua – 博客频道 – CSDN.NET

去在文件顶部加上:

#!/usr/bin/python
# -*- coding: UTF-8 -*- 

即可。

#!/usr/bin/python
# -*- coding: UTF-8 -*-  
from flask import Flask
from wechat_sdk import WechatConf
from wechat_sdk import WechatBasic

此处,即可正常运行Python代码:

(SIPEvents) ➜  SIPEvents gunicorn -w 4 -b 127.0.0.1:8080 wechat_auth:app
[2016-08-18 20:50:36 +0000] [22309] [INFO] Starting gunicorn 19.6.0
[2016-08-18 20:50:36 +0000] [22309] [INFO] Listening at: http://127.0.0.1:8080 (22309)
[2016-08-18 20:50:36 +0000] [22309] [INFO] Using worker: sync
[2016-08-18 20:50:36 +0000] [22314] [INFO] Booting worker with pid: 22314
[2016-08-18 20:50:36 +0000] [22316] [INFO] Booting worker with pid: 22316
[2016-08-18 20:50:36 +0000] [22318] [INFO] Booting worker with pid: 22318
[2016-08-18 20:50:36 +0000] [22321] [INFO] Booting worker with pid: 22321

转载请注明:在路上 » [已解决]Python代码运行出错:SyntaxError: Non-ASCII character xe5 in file on line 12 but no encoding declared

发表我的评论
取消评论

表情

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址
82 queries in 0.175 seconds, using 22.19MB memory