请教一个pip install 出现报asciii码错误的问题。

2013-11-26 12:27:20 +08:00
 burgleaf
系统版本:win8.1 x64
python版本:2.7.5
pip版本:pip-1.4.1
异常如下:
---------------------------------------------------------------------------------------------------------
>pip install sh
Downloading/unpacking sh
Cleaning up...
Exception:
Traceback (most recent call last):
File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\basecomm
and.py", line 134, in main
status = self.run(options, args)
File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\commands
\install.py", line 236, in run
requirement_set.prepare_files(finder, force_root_egg_info=self.bundle, bundl
e=self.bundle)
File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\req.py",
line 1092, in prepare_files
self.unpack_url(url, location, self.is_download)
File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\req.py",
line 1238, in unpack_url
retval = unpack_http_url(link, location, self.download_cache, self.download_
dir)
File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\download
.py", line 621, in unpack_http_url
temp_location = os.path.join(temp_dir, filename)
File "D:\developer\Python27\lib\ntpath.py", line 108, in join
path += "\\" + b
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb1 in position 9: ordinal
not in range(128)

Traceback (most recent call last):
File "D:\developer\Python27\Scripts\pip-script.py", line 9, in <module>
load_entry_point('pip==1.4.1', 'console_scripts', 'pip')()
File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\__init__
.py", line 148, in main
return command.main(args[1:], options)
File "D:\developer\Python27\lib\site-packages\pip-1.4.1-py2.7.egg\pip\basecomm
and.py", line 169, in main
text = '\n'.join(complete_log)
UnicodeDecodeError: 'ascii' codec can't decode byte 0xb1 in position 34: ordinal
not in range(128)
-------------------------------------------------------------------------------------------------------

不知道是什么原因,以前pip是好使的。突然有一天我兴致冲冲地想用一下python就出现这个问题了。不止是安装sh,安装Django,安装任何包都出现这个问题安装不了,网上搜也搜不出答案,所以只好把问题贴在这求大神们指教。
32196 次点击
所在节点    Python
8 条回复
c19
2013-11-26 15:41:54 +08:00
burgleaf
2013-11-26 15:56:20 +08:00
Python 2.7.5 (default, May 15 2013, 22:44:16) [MSC v.1500 64 bit (AMD64)] on wi
32
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'

>>> s1 = s.decode('utf-8')
>>> print s1
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'gbk' codec can't encode character u'\uff61' in position 1:
illegal multibyte sequence

@c19 谢谢你。玩python时间不长,以上是我的输出,应该是和这个帖子一样的问题吧。应该怎么破?
c19
2013-11-26 19:36:18 +08:00
@burgleaf ***见stackoverflow被采纳答案。***
print尝试把unicode编码成环境编码。你得环境编码是GBK。windows?
Linux临时环境变量设置如下:
c19@acer:~$ export LANG=zh_CN.GBK
c19@acer:~$ python
Python 2.7.5+ (default, Sep 19 2013, 13:48:49)
[GCC 4.8.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> s = '(\xef\xbd\xa1\xef\xbd\xa5\xcf\x89\xef\xbd\xa5\xef\xbd\xa1)\xef\xbe\x89'
>>> s1 = s.decode('utf-8')
>>> s1
u'(\uff61\uff65\u03c9\uff65\uff61)\uff89'
>>> print(s1)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-5: ordinal not in range(128)
>>>
burgleaf
2013-11-26 21:58:02 +08:00
@c19 灰常感谢!原因是pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码。解决办法是:
python目录 Python27\Lib\site-packages 建一个文件sitecustomize.py
内容写:
import sys
sys.setdefaultencoding('gb2312')
python会自动运行这个文件。
pfe361c
2013-11-27 11:01:33 +08:00
mark
tanlianming
2013-12-17 16:37:10 +08:00
mark
stargazer
2014-08-26 14:37:20 +08:00
mark
hongfeiyu
2016-01-01 21:25:58 +08:00
赞赞赞,来到这我解决了心头大患

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/90659

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX