以前pip都是好用的,今天装flask的时候他莫名报了个错
Downloading/unpacking flask
Cleaning up...
Exception:
Traceback (most recent call last):
File "D:\Python\lib\site-packages\pip-1.5.6-py2.7.egg\pip\basecommand.py", line 122, in main
status = self.run(options, args)
File "D:\Python\lib\site-packages\pip-1.5.6-py2.7.egg\pip\commands\install.py", line 278, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundle=self.bundle)
File "D:\Python\lib\site-packages\pip-1.5.6-py2.7.egg\pip\req.py", line 1197,in prepare_files
do_download,
File "D:\Python\lib\site-packages\pip-1.5.6-py2.7.egg\pip\req.py", line 1375,in unpack_url
self.session,
File "D:\Python\lib\site-packages\pip-1.5.6-py2.7.egg\pip\download.py", line 571, in unpack_http_url
temp_location = os.path.join(temp_dir, filename)
File "D:\Python\lib\ntpath.py", line 83, in join
result_path = result_path + p_path
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5 in position 7: ordinalnot in range(128)
Traceback (most recent call last):
File "D:\Python\Scripts\pip-script.py", line 9, in
load_entry_point('pip==1.5.6', 'console_scripts', 'pip')()
File "D:\Python\lib\site-packages\pip-1.5.6-py2.7.egg\pip\__init__.py", line 185, in main
return command.main(cmd_args)
File "D:\Python\lib\site-packages\pip-1.5.6-py2.7.egg\pip\basecommand.py", line 161, in main
text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5 in position 34: ordinal not in range(128)
主要还是这一句
UnicodeDecodeError: 'ascii' codec can't decode byte 0xd5 in position 34: ordinal not in range(128)
是编码的问题,这个以前是遇到过的,在刚安装pip的时候也这样,那时候在python安装路径的Lib\site-packages下放了个sitecustomize.py就搞定了,内容是加上编码设定:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
但这次好像没起作用,再看错误里这句
Traceback (most recent call last):
File "D:\Python\lib\site-packages\pip-1.5.6-py2.7.egg\pip\basecommand.py", line 122, in main
status = self.run(options, args)
最外层的调用入口是basecommand.py,于是就去basecommand.py里同样加上了这两句:
import sys
reload(sys)
sys.setdefaultencoding('utf-8')
然后再跑又报错说:
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xd5 in position 34: ordinal not in range(128)
他说utf-8也不行,现在基本我可以确定是我的某些路径有中文字符,而且还不是utf-8的,于是在basecommand.py里写上
import sys
reload(sys)
sys.setdefaultencoding('gbk')
ok,搞定了。
这中间还有一次遇到一个问题是我原来只写了
import sys
sys.setdefaultencoding('utf-8')
他会报一个这样的错,
AttributeError: 'module' object has no attribute 'setdefaultencoding'
说没有setdefaultencoding这个东西,这也是我在编辑sys.setdefaultencoding时为什么老是得不到提示的原因,查了一下,就是说早些版本的python是只要写这两句就可以了,而较高的python必须要reload(sys)一下。