V2EX = way to explore
V2EX 是一个关于分享和探索的地方
现在注册
已注册用户请  登录
推荐学习书目
Learn Python the Hard Way
Python Sites
PyPI - Python Package Index
http://diveintopython.org/toc/index.html
Pocoo
值得关注的项目
PyPy
Celery
Jinja2
Read the Docs
gevent
pyenv
virtualenv
Stackless Python
Beautiful Soup
结巴中文分词
Green Unicorn
Sentry
Shovel
Pyflakes
pytest
Python 编程
pep8 Checker
Styles
PEP 8
Google Python Style Guide
Code Style from The Hitchhiker's Guide
Pooc
V2EX  ›  Python

求助: win10 下新建 flask,运行报错 UnicodeDecodeError 的问题

  •  
  •   Pooc · 2018-05-22 10:32:58 +08:00 · 3359 次点击
    这是一个创建于 2378 天前的主题,其中的信息可能已经有所发展或是发生改变。

    配置好环境学习 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
    
    John60676
        1
    John60676  
       2018-05-22 11:57:49 +08:00   ❤️ 1
    # encoding: utf-8
    文件头加上这段试试
    Pooc
        2
    Pooc  
    OP
       2018-05-22 12:56:20 +08:00
    @John60676 谢谢你的解答,不过还是不行。。。
    John60676
        3
    John60676  
       2018-05-22 16:43:25 +08:00   ❤️ 1
    ```
    import sys
    reload(sys)
    sys.setdefaultencoding('utf-8')
    ```
    再试试这段,也是放在头部。

    如果不行的话,看看你计算机名是不是中文的,改成英文试试。
    Pooc
        4
    Pooc  
    OP
       2018-05-22 21:57:48 +08:00
    @John60676 !!!的确是计算机名字为中文的问题!修改就好了,多谢,多谢!!!
    关于   ·   帮助文档   ·   博客   ·   API   ·   FAQ   ·   实用小工具   ·   6051 人在线   最高记录 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 41ms · UTC 02:31 · PVG 10:31 · LAX 18:31 · JFK 21:31
    Developed with CodeLauncher
    ♥ Do have faith in what you're doing.