pip安装python包时出现编码错误

很久之前遇到的问题,pip安装python包时报错如下:
UnicodeEncodeError: ‘ascii’ codec can’t encode character u’\u258f’ in position 8
: ordinal not in range(128)
后来换了easy install也就没有管他了,今天又遇到了这个问题,觉得还是要解决一下的。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
Exception:
Traceback (most recent call last):
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\basecommand.py", l
ine 215, in main
status = self.run(options, args)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\commands\install.p
y", line 299, in run
requirement_set.prepare_files(finder)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\req\req_set.py", l
ine 370, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\req\req_set.py", l
ine 587, in _prepare_file
session=self.session, hashes=hashes)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\download.py", line
810, in unpack_url
hashes=hashes
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\download.py", line
649, in unpack_http_url
hashes)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\download.py", line
871, in _download_http_url
_download_url(resp, link, content_file, hashes)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\download.py", line
595, in _download_url
hashes.check_against_chunks(downloaded_chunks)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\utils\hashes.py",
line 46, in check_against_chunks
for chunk in chunks:
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\download.py", line
563, in written_chunks
for chunk in chunks:
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\utils\ui.py", line
141, in iter
self.next(n)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\_vendor\progress\_
_init__.py", line 73, in next
self.update()
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\_vendor\progress\b
ar.py", line 79, in update
self.writeln(line)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\_vendor\progress\h
elpers.py", line 68, in writeln
print(line, end='', file=self.file)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\_vendor\colorama\a
nsitowin32.py", line 141, in write
self.write_and_convert(text)
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\_vendor\colorama\a
nsitowin32.py", line 169, in write_and_convert
self.write_plain_text(text, cursor, len(text))
File "c:\python27\lib\site-packages\pip-8.1.2-py2.7.egg\pip\_vendor\colorama\a
nsitowin32.py", line 174, in write_plain_text
self.wrapped.write(text[start:end])
UnicodeEncodeError: 'ascii' codec can't encode character u'\u258f' in position 8
: ordinal not in range(128)

此处 得知原因如下

pip安装python包会加载我的用户目录,我的用户目录恰好是中文的,ascii不能编码。解决办法是:
python目录 Python27\Lib\site-packages 建一个文件sitecustomize.py
内容写:

import sys
sys.setdefaultencoding(‘gb2312’)

python会自动运行这个文件。

解决方案为在 Python27\Lib\site-packages下建立一个文件sitecustomize.py写入:

import sys
sys.setdefaultencoding(‘gb2312’) #我这样修改后并不可以,修改gb2312为UTF-8后解决