在Windows下使用Pycharm启动应用程序时出现UnicodeDecodeError

时间:2019-04-21 11:52:28

标签: python python-3.x flask

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

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

整个文件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上读到了这可以有所帮助:

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

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

app.run()

app.run(host='127.0.0.1')

但它也不起作用。

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

set FLASK_APP = app.py

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

flask run

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

完整错误代码:

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位置和项目位置,并且错误仍然存​​在:

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

2 个答案:

答案 0 :(得分:0)

问题是本机字母ł(可能是名称Michał),其代码为0xb3,但代码为Window-1250,而不是UTF-8

也许您不仅在文件夹名称中有此字母,而且在主机名(计算机名称)中也有此字母,然后脚本尝试在其中获取此名称时脚本出现问题

hostname, aliases, ipaddrs = gethostbyaddr(name)

您可以看到此行错误消息。

也许如果您在Windows的设置中更改主机名,它将起作用。


我不使用Windows,但是当我使用Windows(和DOS)时,总是有一个很好的规则:不要在文件/文件夹名称中使用本机(波兰语)字母(和空格)。主机名也可以如此。


Windows可能使用三种不同的编码(代码页)-Window-1250CP-1250)用于文件名,CP-852用于控制台中的文本,脚本中的UTF-8。有时也会在旧的HTML文件中LATIN2ISO-8859-2)。

请参阅:在页面Python Unicode - decode & encode 上精加工代码页(编码)的图像

答案 1 :(得分:0)

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

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