配置好环境学习 flask,直接运行出现
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 0: invalid start byte
报错问题 ,看完报错提示一脸懵逼,然后以为是环境没配好,使用
conda create -n your_env_name python=3.6
创建新的环境还是如此,百度 Google 无果后,于是想请求下大家,应该如何解决?
代码部分:
from flask import Flask
app = Flask(__name__)
@app.route('/')
def hello_world():
return 'Hello World!'
if __name__ == '__main__':
app.run()
报错内容:
D:\ProgramData\Anaconda3\python.exe D:/Work/flask_demo/flask_demo.py
* Serving Flask app "flask_demo" (lazy loading)
* Environment: production
WARNING: Do not use the development server in a production environment.
Use a production WSGI server instead.
* Debug mode: off
Traceback (most recent call last):
File "D:/Work/flask_demo/flask_demo.py", line 12, in <module>
app.run()
File "D:\ProgramData\Anaconda3\lib\site-packages\flask\app.py", line 943, in run
run_simple(host, port, self, **options)
File "D:\ProgramData\Anaconda3\lib\site-packages\werkzeug\serving.py", line 814, in run_simple
inner()
File "D:\ProgramData\Anaconda3\lib\site-packages\werkzeug\serving.py", line 774, in inner
fd=fd)
File "D:\ProgramData\Anaconda3\lib\site-packages\werkzeug\serving.py", line 660, in make_server
passthrough_errors, ssl_context, fd=fd)
File "D:\ProgramData\Anaconda3\lib\site-packages\werkzeug\serving.py", line 577, in __init__
self.address_family), handler)
File "D:\ProgramData\Anaconda3\lib\socketserver.py", line 453, in __init__
self.server_bind()
File "D:\ProgramData\Anaconda3\lib\http\server.py", line 138, in server_bind
self.server_name = socket.getfqdn(host)
File "D:\ProgramData\Anaconda3\lib\socket.py", line 673, in getfqdn
hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xbf in position 0: invalid start byte
1
John60676 2018-05-22 11:57:49 +08:00 1
# encoding: utf-8
文件头加上这段试试 |
3
John60676 2018-05-22 16:43:25 +08:00 1
```
import sys reload(sys) sys.setdefaultencoding('utf-8') ``` 再试试这段,也是放在头部。 如果不行的话,看看你计算机名是不是中文的,改成英文试试。 |