python - 如何修复 UnicodeDecodeError: 'utf-8' codec can't decode byte 0xcf in position 5: invalid continuation byte
问题描述
我正在尝试在服务器上运行新创建的环境并获取 UnicodeDecodeError。
Windows 7,64 位。我创建了一个虚拟环境并将代码定向到源文件夹。这里安装了 django、python 和 virtualenv 版本。我尝试安装旧版本的 django 和 python 并得到同样的错误。
在搜索故障排除建议时,我只找到与 .csv 和 pandas 相关的文章。
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
您能帮我找出解决此错误消息的方法吗?
PS 这是我的第一个 web 框架项目,我对 python 有一些了解。我非常感谢提供的任何帮助。我想建立自己的网站。
解决方案
您的主机文件中有别名,其中包含无法解码为 UTF-8 的字符。删除它们以解决问题(将任何别名设为仅 ASCII 可能是最简单的)。
Windows 7 上主机文件的默认位置是
C:\Windows\System32\Drivers\etc\hosts
推荐阅读
- c# - 拦截 SqlServerMigrationsSqlGenerator 以防止某些表的迁移?
- c# - 如何确定 String.Split() 中使用的字符
- c# - cshtml中的多对多关系没有获取数据
- python - 元组的问题
- arrays - 引用单元格数据作为工作表名称源的一部分的 Google 工作表查询
- keep-alive - WireMock 保持活动状态
- objective-c - 如何使用 Objective-C 以编程方式将角半径添加到搜索栏?
- curl - curl --doh-url 是否不接受 dns 服务器的 IP 地址,并且响应只是 html?
- javascript - Angular - TypeError:无法读取未定义的属性“0”
- tensorflow - 自定义卷积层的梯度是无的