在Windows下使用Pycharm启动应用程序时出现UnicodeDecodeError [英] UnicodeDecodeError while starting the app under Windows with Pycharm

查看:84
本文介绍了在Windows下使用Pycharm启动应用程序时出现UnicodeDecodeError的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

问题是当我尝试启动应用程序(app.py)时我收到以下错误:

The problem is that when i try to start the application (app.py) i get the following error:

UnicodeDecodeError:"utf-8"编解码器无法解码位置5的字节0xb3:无效的起始字节

UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 5: invalid start byte

整个文件app.py:

The whole file app.py:

# -*- coding: utf-8 -*-
from flask import Flask

app = Flask(__name__)

@app.route("/")
def hello():
    return 'Hello World'

if __name__ == "__main__":
    app.run(host='127.0.0.1')

我在此处的stackoverflow上读到了这可以有所帮助:

I read somewhere here on stackoverflow that this can help:

# -*- coding: utf-8 -*-  

但是它没有任何改变.然后我从

but it doesn't change anything. Then i changed from

app.run()

app.run(host='127.0.0.1')

但它也不起作用.

我在Windows下启动该应用程序,因此我设置了环境.变量如下:

I start the app under windows, so i set the env. variable as follows:

set FLASK_APP = app.py

但是无论我是在pycharm中还是在Windows cmd中运行

but no matter if i run this in pycharm or in windows cmd with

flask run

我每次都会遇到相同的错误.

I get the same error everytime.

完整错误代码:

Traceback (most recent call last):
  File "C:/Users/Michał/Desktop/Michał/Zadanie/Flask_Blog/app.py", 
line 11, in <module>
    app.run(host='127.0.0.1')
  File "C:\Users\Michał\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\flask\app.py", line 943, in run
    run_simple(host, port, self, **options)
  File "C:\Users\Michał\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\werkzeug\serving.py", line 990, in run_simple
    inner()
  File "C:\Users\Michał\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\werkzeug\serving.py", line 943, in inner
    fd=fd,
  File "C:\Users\Michał\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\werkzeug\serving.py", line 786, in make_server
    host, port, app, request_handler, passthrough_errors, ssl_context, 
fd=fd
  File "C:\Users\Michał\AppData\Local\Programs\Python\Python36- 
32\lib\site-packages\werkzeug\serving.py", line 679, in __init__
    HTTPServer.__init__(self, server_address, handler)
  File "C:\Users\Michał\AppData\Local\Programs\Python\Python36- 
32\lib\socketserver.py", line 453, in __init__
    self.server_bind()
  File "C:\Users\Michał\AppData\Local\Programs\Python\Python36- 
32\lib\http\server.py", line 138, in server_bind
    self.server_name = socket.getfqdn(host)
  File "C:\Users\Michał\AppData\Local\Programs\Python\Python36- 
32\lib\socket.py", line 673, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 5: 
invalid start byte

编辑(更新)我更改了python.exe位置和项目位置,并且错误仍然存​​在:

Edit (Update) I changed the python.exe location and project location and the error still exist:

Traceback (most recent call last):
  File "C:/Users/Public/Projekt/main.py", line 11, in <module>
    app.run(host='127.0.0.1')
  File "C:\Users\Public\Python\Python36-32\lib\site- 
packages\flask\app.py", line 943, in run
    run_simple(host, port, self, **options)
  File "C:\Users\Public\Python\Python36-32\lib\site-
packages\werkzeug\serving.py", line 990, in run_simple
    inner()
  File "C:\Users\Public\Python\Python36-32\lib\site-packages\werkzeug\serving.py", line 943, in inner
    fd=fd,
  File "C:\Users\Public\Python\Python36-32\lib\site-packages\werkzeug\serving.py", line 786, in make_server
    host, port, app, request_handler, passthrough_errors, ssl_context, fd=fd
  File "C:\Users\Public\Python\Python36-32\lib\site-packages\werkzeug\serving.py", line 679, in __init__
    HTTPServer.__init__(self, server_address, handler)
  File "C:\Users\Public\Python\Python36-32\lib\socketserver.py", line 453, in __init__
    self.server_bind()
  File "C:\Users\Public\Python\Python36-32\lib\http\server.py", line 138, in server_bind
    self.server_name = socket.getfqdn(host)
  File "C:\Users\Public\Python\Python36-32\lib\socket.py", line 673, in getfqdn
    hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 5: invalid start byte

推荐答案

方法 gethostbyaddr 将获取计算机的名称,如果不是英语,则cmd将导致此错误.

The method gethostbyaddr will get the name of your computer, if it's not English, cmd will result in this error.

您需要完全用英语重命名Windows计算机,然后重新启动计算机

You need to rename your windows computer by english totally, and reboot your computer

这篇关于在Windows下使用Pycharm启动应用程序时出现UnicodeDecodeError的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆