如何修复UnicodeDecodeError:'utf-8'编解码器无法解码位置5的字节0xcf:无效的连续字节

时间:2019-11-05 11:52:26

标签: python django unicode utf-8 decoder

我正在尝试在服务器上运行新创建的环境并获取UnicodeDecodeError。

Windows 7,64位。我创建了一个虚拟环境并将代码定向到源文件夹。这里是安装的django,python和virtualenv版本。我尝试安装django和python的旧版本,并得到了相同的错误。

搜索故障排除建议时,我只会找到与.csv和熊猫相关的文章。

Django==2.2.7
pytz==2019.3
virtualenv==16.7.7
PS: C:\Users\User\Dev\Selectia\src> python manage.py runserver

以下是输出:

Watching for file changes with StartReloader
Performing system checks...

System checkidentified no issues (0 silenced).
November 05, 2019 - 12:02:31
Django version 2.2.7, using setings 'foodie.settings'
Starting development server at http://127.0.0.1:8000/
Quit the server with CTRL-BREALK.
Exception in thread django-main-thread:
Traceback (most recent call last):
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 917, in _bootstrap_inner
   self.run()
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\threading.py", line 865, in run
  self._target(*self.args, **self._kwargs)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\utils\autoreload.py", line 54 in wrapper 
  fn(*args, **kwargs)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 203, in run
  httpd = httpd_cls(server_address, WSGIRequestHandler, ipv6=ipv6)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\site-packages\django\core\servers\basehttp.py", line 67, in __init__
  super().__init__(*args, **kwargs)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\socketserver.py", line 452, in __init__
  self.server_bind()
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\wsgiref\simple_server.py", line 50, in server_bind
  HTTPServer.server_bind(self)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\http\server.py", line 139, in server_bind
  self.server_name = socket.getfqdn(host)
 File "C:\Users\User\AppData\Local\Programs\Python\Python37\lib\socket.py", line 676, in getfqdn
  hostname, aliases, ipaddrs = gethostbyaddr(name)
UnicodeDecoderError: 'utf-8' code can't decode byte 0xcf in position 5: invalid continuation byte

能否请我帮忙找出解决此错误消息的方法?

P.S。这是我的第一个Web框架项目,我对python有一定的了解。我将非常感谢您提供的任何帮助。我想建立自己的网站。

1 个答案:

答案 0 :(得分:0)

您的主机文件中有别名,其中包含无法解码为UTF-8的字符。删除它们可以解决问题(将所有别名设置为仅ASCII可能是最容易的)。

Windows 7上主机文件的默认位置是

 C:\Windows\System32\Drivers\etc\hosts