setuptools " UnicodeDecodeError:"问题探究

本文解析了在Python 2.7.6中安装setuptools或xlrd等包时遇到的UnicodeDecodeError错误的原因,并提供了清理注册表及升级Python版本的解决方案。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值