问题
The problem is that when i try to start the application (app.py) i get the following error:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb3 in position 5: invalid start byte
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')
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()
to
app.run(host='127.0.0.1')
but it doesn't work too.
I start the app under windows, so i set the env. variable as follows:
set FLASK_APP = app.py
but no matter if i run this in pycharm or in windows cmd with
flask run
I get the same error everytime.
Full error code:
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
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
回答1:
Problem is native letter ł
(probably in name Michał
) which has code 0xb3
but in Window-1250
, not UTF-8
.
Maybe you have this letter not only in folder name but also in hostname (computer's name) and then script has problem when it try to get this name in
hostname, aliases, ipaddrs = gethostbyaddr(name)
You can see this line error message.
Maybe if you change hostname in Windows's setting then it will works.
I don't use Windows but when I was using Windows (and DOS) there was always good rule: don't use native (Polish) letters (and spaces) in file/folder name. The same can be with hostname.
Windows may use three different encoding (Code Page) - Window-1250
(CP-1250
) for filenames, CP-852
for text in console and UTF-8
in your script. Sometimes also LATIN2
(ISO-8859-2
) in old HTML files.
See: image which polish code pages (encodings) on page Python Unicode - decode & encode
回答2:
The method gethostbyaddr
will get the name of your computer, if it's not English, cmd will result in this error.
You need to rename your windows computer by english totally, and reboot your computer
来源:https://stackoverflow.com/questions/55782460/unicodedecodeerror-while-starting-the-app-under-windows-with-pycharm