以下为转载,本机遇到的问题解决了
UnicodeDecodeError: 'ascii' codec can't decode byte 0xcb in position 0: ordinal not in range(128) 'UnicodeDecodeError'这个词已经暴露了这个问题是个编码问题
什么原因导致了这样的问题?在我的电脑上出现这个问题的原因是由于我的用户目录是中文的,pip在下载的时候调用了这样一行代码
- temp_dir = tempfile.mkdtemp('-unpack', 'pip-')
那问题要怎么解决呢?有两种方法解决:
1、把上面的temp_dir那段代码修改一个不包含中文的目录,修改这段代码的文件位置在D:\Python27\Lib\site-packages\pip-6.1.1-py2.7.egg\pip\download.py(位置由个人python安装目录决定)
2、修改编码为gbk,修改D:\Python27\Lib\ntpath.py(位置由个人python安装目录决定)文件中的def join(path, *paths)函数,在函数内第一行加入
- # Join two (or more) paths.
- def join(path, *paths):
- """Join two or more pathname components, inserting "\\" as needed."""
- reload(sys)
- sys.setdefaultencoding('gbk')
- result_drive, result_path = splitdrive(path)
- for p in paths:
- p_drive, p_path = splitdrive(p)
- if p_path and p_path[0] in '\\/':
- # Second path is absolute
- if p_drive or not result_drive:
- result_drive = p_drive
- result_path = p_path
- continue
- elif p_drive and p_drive != result_drive:
- if p_drive.lower() != result_drive.lower():
- # Different drives => ignore the first path entirely
- result_drive = p_drive
- result_path = p_path
- continue
- # Same drive in different case
- result_drive = p_drive
- # Second path is relative to the first
- if result_path and result_path[-1] not in '\\/':
- result_path = result_path + '\\'
- result_path = result_path + p_path
- ## add separator between UNC and non-absolute path
- if (result_path and result_path[0] not in '\\/' and
- result_drive and result_drive[-1:] != ':'):
- return result_drive + sep + result_path
- return result_drive + result_path
- reload(sys)
- sys.setdefaultencoding('gbk')
一切准备就绪,重新执行pip安装试试吧
总结:
1、据说python3的默认编码为'utf-8',可能不存在这种问题,没有实际测试过
2、这次我直接修改了python和pip中的源码,体现了python是脚本语言的特性
3、如果本文对您有用,请支持原创,谢谢