Q:在安装setuptools时Python ez_setup.py,或者安装其他egg包,有时候会碰到: UnicodeDecodeError错误,这是为什么呢?
安装setuptools时UnicodeDecodeError错误
<span style="font-family:SimSun;"> mimetypes.init() # try to read system mime.types File "C:\Python27\lib\mimetypes.py", line 358, in init db.read_windows_registry() File "C:\Python27\lib\mimetypes.py", line 258, in read_windows_registry for subkeyname in enum_types(hkcr): File "C:\Python27\lib\mimetypes.py", line 249, in enum_types ctype = ctype.encode(default_encoding) # omit in 3.x! UnicodeDecodeError: 'ascii' codec can't decode byte 0x83 in position 8: ordinal not in range(128) Something went wrong during the installation. See the error message above.</span>
安装xlrd时UnicodeDecodeError错误
<span style="font-family:SimSun;"> mimetypes.init() # try to read system mime.types File "C:\Python27\lib\mimetypes.py", line 358, in init db.read_windows_registry() File "C:\Python27\lib\mimetypes.py", line 258, in read_windows_registry for subkeyname in enum_types(hkcr): File "C:\Python27\lib\mimetypes.py", line 249, in enum_types ctype = ctype.encode(default_encoding) # omit in 3.x! UnicodeDecodeError: 'ascii' codec can't decode byte 0x88 in position 1: ordinal not in range(128)</span>
UnicodeDecodeError错误原因分析
从Traceback来看是函数mimetypes.init(),在进行ascil解码时,抛出了异常了,查看mimetypes.py源代码,下面是错误发生的地方。
<span style="font-family:SimSun;">default_encoding = sys.getdefaultencoding() with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr: for subkeyname in enum_types(hkcr): try: with _winreg.OpenKey(hkcr, subkeyname) as subkey: # Only check file extensions if not subkeyname.startswith("."): continue # raises EnvironmentError if no 'Content Type' value mimetype, datatype = _winreg.QueryValueEx( subkey, 'Content Type') if datatype != _winreg.REG_SZ: continue try: mimetype = mimetype.encode(default_encoding) subkeyname = subkeyname.encode(default_encoding) except UnicodeEncodeError: continue self.add_type(mimetype, subkeyname, strict)</span>
我想现在大家已经明白了UnicodeDecodeError错误原因,就是注册表HKEY_CLASSES_ROOT下的子key存在不能用当前系统默认的asci编码解码时抛出UnicodeEncodeError错误。
建议解决策:
- 清理注册表HKEY_CLASSES_ROOT中,含有当前ASCI以外字符的key
- 抛弃Python2.7.6,改用Python2.7.7
P.S这是python2.7.6的一个bug,Python2.7.7已经修改了。
http://bugs.python.org/issue9291
setuptools " UnicodeDecodeError:"问题探究
时间: 2024-11-05 09:19:25