近日升级和安装python的包时总是出现类似的错误


Upgrade packages failed.
The following command was executed:
packaging_tool.py install --build-dir C:\Users\admin\AppData\Local\Temp\pycharm-packaging7494122386570496301.tmp -U selenium
The error output of the command:
Downloading/unpacking selenium from https://pypi.python.org/packages/source/s/selenium/selenium-2.38.3.tar.gz#md5=dae21e5d9ce2f286cb6e2c3f653e9aaa
  Running setup.py egg_info for package selenium
    Traceback (most recent call last):
      File "<string>", line 3, in <module>
      File "D:\Python27\lib\site-packages\setuptools\__init__.py", line 11, in <module>
        from setuptools.extension import Extension
      File "D:\Python27\lib\site-packages\setuptools\extension.py", line 5, in <module>
        from setuptools.dist import _get_unpatched
      File "D:\Python27\lib\site-packages\setuptools\dist.py", line 15, in <module>
        from setuptools.compat import numeric_types, basestring
      File "D:\Python27\lib\site-packages\setuptools\compat.py", line 19, in <module>
        from SimpleHTTPServer import SimpleHTTPRequestHandler
      File "D:\Python27\lib\SimpleHTTPServer.py", line 27, in <module>
        class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
      File "D:\Python27\lib\SimpleHTTPServer.py", line 208, in SimpleHTTPRequestHandler
        mimetypes.init() # try to read system mime.types
      File "D:\Python27\lib\mimetypes.py", line 358, in init
        db.read_windows_registry()
      File "D:\Python27\lib\mimetypes.py", line 258, in read_windows_registry
        for subkeyname in enum_types(hkcr):
      File "D:\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 0xb0 in position 1: ordinal not in range(128)
    Complete output from command python setup.py egg_info:
    Traceback (most recent call last):
  File "<string>", line 3, in <module>
  File "D:\Python27\lib\site-packages\setuptools\__init__.py", line 11, in <module>
    from setuptools.extension import Extension
  File "D:\Python27\lib\site-packages\setuptools\extension.py", line 5, in <module>
    from setuptools.dist import _get_unpatched
  File "D:\Python27\lib\site-packages\setuptools\dist.py", line 15, in <module>
    from setuptools.compat import numeric_types, basestring
  File "D:\Python27\lib\site-packages\setuptools\compat.py", line 19, in <module>
    from SimpleHTTPServer import SimpleHTTPRequestHandler
  File "D:\Python27\lib\SimpleHTTPServer.py", line 27, in <module>
    class SimpleHTTPRequestHandler(BaseHTTPServer.BaseHTTPRequestHandler):
  File "D:\Python27\lib\SimpleHTTPServer.py", line 208, in SimpleHTTPRequestHandler
    mimetypes.init() # try to read system mime.types
  File "D:\Python27\lib\mimetypes.py", line 358, in init
    db.read_windows_registry()
  File "D:\Python27\lib\mimetypes.py", line 258, in read_windows_registry
    for subkeyname in enum_types(hkcr):
  File "D:\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 0xb0 in position 1: ordinal not in range(128)
----------------------------------------
Cleaning up...
Command python setup.py egg_info failed with error code 1 in C:\Users\admin\AppData\Local\Temp\pycharm-packaging7494122386570496301.tmp\selenium
Storing complete log in C:\Users\admin\pip\pip.log


通过研究发现,安装时会检查HKEY_CLASSES_ROOT的子键,而邪恶的旺旺每次启动时都会在HKEY_CLASSES_ROOT增加一个'.阿里旺旺接收的可疑文件'键值,这个键值是中文的,就会读取时导致如下错误


    mimetypes.init() # try to read system mime.types
  File "D:\Python27\lib\mimetypes.py", line 358, in init
    db.read_windows_registry()
  File "D:\Python27\lib\mimetypes.py", line 258, in read_windows_registry
    for subkeyname in enum_types(hkcr):
  File "D:\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 0xb0 in position 1: ordinal not in range(128)


解决办法,删掉就行了,如果每次删掉比较麻烦,用autoit来删除,代码如下


RegDelete('HKEY_CLASSES_ROOT\.阿里旺旺接收的可疑文件')


编译好的exe也可以在附件中下载


20131226更新:

不只是阿里旺旺会导致出错,只要是注册了中文后缀名都有可能出错,另外每次改也不是一个事儿,推荐一个一劳永逸的方法


进入python安装目录下的Lib目录,打开mimetypes.py文件


在default_encoding = sys.getdefaultencoding()前添加

       # begin

       if sys.getdefaultencoding() != 'gbk':

           reload(sys)

           sys.setdefaultencoding('gbk')

       # end

# begin
if sys.getdefaultencoding() != 'gbk':
    reload(sys)
    sys.setdefaultencoding('gbk')
# end
default_encoding = sys.getdefaultencoding()
with _winreg.OpenKey(_winreg.HKEY_CLASSES_ROOT, '') as hkcr: