[Python2] Win7(x64)下安装Py2.7、pip,以及错误UnicodeEncodeError的解决方案


环境

Win7 (x64) 中文


安装Python以及Pip


使用Pip安装扩展库

比如安装requests库:pip install requests

报错如下:

…省略…
File “C:\Python27\lib\site-packages\pip-9.0.1-py2.7.egg\pip_vendor\colorama\ansitowin32.py”, line 174, in write_plain_text
self.wrapped.write(text[start:end])
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u258b’ in position 8: ordinal not in range(128)

解决方案:

找到文件ansitowin32.py,并在文件开头加上

import sys
reload(sys)
sys.setdefaultencoding(‘gbk’) #gbk编码在cmd下使用pip install进度条正常显示,此处需根据自己需求更改该编码

此处特地标红了reload(sys)语句,如果不加这条语句,会出现错误:

AttributeError: ‘module’ object has no attribute ‘setdefaultencoding’

先看Python2.7模块加载过程:

 C:\Documents and Settings\Administrator\桌面>python -v
# installing zipimport hook
import zipimport # builtin
# installed zipimport hook
# C:\Python27\lib\site.pyc matches C:\Python27\lib\site.py
import site # precompiled from C:\Python27\lib\site.pyc
# C:\Python27\lib\os.pyc matches C:\Python27\lib\os.py
import os # precompiled from C:\Python27\lib\os.pyc
import errno # builtin
import nt # builtin
# C:\Python27\lib\ntpath.pyc matches C:\Python27\lib\ntpath.py
import ntpath # precompiled from C:\Python27\lib\ntpath.pyc
# C:\Python27\lib\stat.pyc matches C:\Python27\lib\stat.py
import stat # precompiled from C:\Python27\lib\stat.pyc
# C:\Python27\lib\genericpath.pyc matches C:\Python27\lib\genericpath.py
import genericpath # precompiled from C:\Python27\lib\genericpath.pyc
# C:\Python27\lib\warnings.pyc matches C:\Python27\lib\warnings.py
import warnings # precompiled from C:\Python27\lib\warnings.pyc

首先会加载C:\Python2.7\lib\site.py文件,在site.py文件中的main函数最后两句代码:

 def main():
    global ENABLE_USER_SITE
    abs__file__()
    known_paths = removeduppaths()
    if (os.name == "posix" and sys.path and
        os.path.basename(sys.path[-1]) == "Modules"):
        addbuilddir()
    if ENABLE_USER_SITE is None:
        ENABLE_USER_SITE = check_enableusersite()
    known_paths = addusersitepackages(known_paths)
    known_paths = addsitepackages(known_paths)
    if sys.platform == 'os2emx':
        setBEGINLIBPATH()
    setquit()
    setcopyright()
    sethelper()
    aliasmbcs()
    setencoding()
    execsitecustomize()
    if ENABLE_USER_SITE:
        execusercustomize()
    # Remove sys.setdefaultencoding() so that users cannot change the encoding after initialization. The test for presence is needed when this module is run as a script, because this code is executed twice.
     if hasattr(sys, "setdefaultencoding"):
        del sys.setdefaultencoding

在加载sys后,setdefaultencoding函数被删除了,所以需要重新加载sys。

一劳永逸的方案

在python的Lib\site-packages文件夹下新建一个sitecustomize.py,内容为:

import sys
reload(sys)
sys.setdefaultencoding(‘gbk’)

python启动的时候会调用sitecustomize.py文件设置系统的默认编码,而不需要每次都手动的在错误文件上加这三行代码。


Reference

  1. http://liguangming.com/how-to-use-utf-8-with-python
  2. http://blog.csdn.net/intel80586/article/details/8566057

转载自:https://blog.csdn.net/Form_/article/details/79177478

You may also like...