UnicodeDecodeError: 'utf8' codec can't decode byte 0xcc in position 0: invalid continuation byte

2015-09-04 23:52:38 +08:00
 mingyun
windows python2.7 执行 pip -h 然后报错如何,怎么破?
$ pip -h
Traceback (most recent call last ):
File "d:\python2\Scripts\pip-script.py", line 9, in <module>
load_entry_point ('pip==7.1.2', 'console_scripts', 'pip')()
File "d:\python2\lib\site-packages\distribute-0.6.49-py2.7.egg\pkg_resources.p
y", line 345, in load_entry_point
return get_distribution (dist ).load_entry_point (group, name )
File "d:\python2\lib\site-packages\distribute-0.6.49-py2.7.egg\pkg_resources.p
y", line 2381, in load_entry_point
return ep.load ()
File "d:\python2\lib\site-packages\distribute-0.6.49-py2.7.egg\pkg_resources.p
y", line 2087, in load
entry = __import__(self.module_name, globals (),globals (), ['__name__'])
File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\__init__.py", line
15, in <module>
from pip.vcs import git, mercurial, subversion, bazaar # noqa
File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\vcs\mercurial.py",
line 10, in <module>
from pip.download import path_to_url
File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\download.py", line
38, in <module>
from pip._vendor import requests, six
File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\__
init__.py", line 53, in <module>
from .packages.urllib3.contrib import pyopenssl
File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\pa
ckages\__init__.py", line 3, in <module>
from . import urllib3
File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\pa
ckages\urllib3\__init__.py", line 10, in <module>
from .connectionpool import (
File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\pa
ckages\urllib3\connectionpool.py", line 31, in <module>
from .connection import (
File "d:\python2\lib\site-packages\pip-7.1.2-py2.7.egg\pip\_vendor\requests\pa
ckages\urllib3\connection.py", line 9, in <module>
from http.client import HTTPConnection as _HTTPConnection, HTTPException
File "d:\python2\http.py", line 2, in <module>
from CGIHTTPServer import CGIHTTPRequestHandler
File "d:\python2\lib\CGIHTTPServer.py", line 30, in <module>
import SimpleHTTPServer
File "d:\python2\lib\SimpleHTTPServer.py", line 27, in <module>
class SimpleHTTPRequestHandler (BaseHTTPServer.BaseHTTPRequestHandler ):
File "d:\python2\lib\SimpleHTTPServer.py", line 208, in SimpleHTTPRequestHandl
er
mimetypes.init () # try to read system mime.types
File "d:\python2\lib\mimetypes.py", line 368, in init
db.read_windows_registry ()
File "d:\python2\lib\mimetypes.py", line 268, in read_windows_registry
for subkeyname in enum_types (hkcr ):
File "d:\python2\lib\mimetypes.py", line 257, in enum_types
ctype = ctype.encode (default_encoding ) # omit in 3.x!
UnicodeDecodeError: 'utf8' codec can't decode byte 0xcc in position 0: invalid c
ontinuation byte
8527 次点击
所在节点    Python
5 条回复
matthewgao
2015-09-05 08:39:11 +08:00
看起来很怪啊, 你可以看下他那个 pip-script 的脚本, 为什么 help 会涉及 simpleHTTPserver 。。。

codec 这个问题,一般就是因为他以为他在 Decode UTF-8 的数据,但是实际上他不是 UTF-8 的
mingyun
2015-09-05 10:38:06 +08:00
@matthewgao 那个文件就 10 行代码
#!d:\python2\python.exe
# EASY-INSTALL-ENTRY-SCRIPT: 'pip==1.5.2','console_scripts','pip'
__requires__ = 'pip==1.5.2'
import sys
from pkg_resources import load_entry_point

if __name__ == '__main__':
sys.exit (
load_entry_point ('pip==1.5.2', 'console_scripts', 'pip')()
)

和这个有关吗?
>>> sys.getdefaultencoding ()
'utf-8'
xiazi
2015-09-05 17:13:31 +08:00
这样的情况一般都是 gbk 被当成 utf-8 decode 了.

应该使用:
import locale
locale.getdefaultlocale ()[1]
mingyun
2015-09-05 21:17:22 +08:00
@xiazi locale.getdefaultlocale ()[1]输出 cp936 该怎么处理呢?
xiazi
2015-09-06 14:07:27 +08:00
升级到 python2.7.10

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/218327

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX