在win10上通过pip安装 PyYAML bcolz时会出现UnicodeDecodeError ,使用 chcp 65001也不起作用
C:\Users\jli>pip --version
pip 9.0.1 from C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages (python 3.6)
这gitbub上已经有fix了,但似乎还没有merge到 pip 9.0.1, 找到pip安装目录,在pip\compat\__init__.py", line 73 替换 console_to_str
,新的代码为
def subprocess_encoding():
if WINDOWS:
if sys.version_info >= (3, 6):
return "oem"
# Prior to Python 3.6, sys.__stdout__ is opened
# with the OEM code page (at least in the console
# interpreter, which is what pip uses). This changed
# in Python 3.6 to be UTF8, but we don't care as we
# use the explicit "oem" encoding in that case via
# the code above
return sys.__stdout__.encoding
else:
import locale
# Note that the use of getpreferredencoding here calls
# setlocale, which isn't thread safe. This is OK, as we
# never call this code in a multi-threaded context.
# If thread safety was important, we could call
# getpreferredencoding(False), but there are apparently
# some systems where that will not give the correct answer.
#
# We fall back to UTF8 if locale can't provide an answer,
# as UTF8 is the most common encoding used nowadays.
return locale.getpreferredencoding() or "utf-8"
def console_to_str(s):
return s.decode(subprocess_encoding(), errors="replace")
C:\Users\jli>pip install bcolz
Collecting bcolz
Using cached bcolz-1.1.2.tar.gz
Exception:
Traceback (most recent call last):
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\compat\__init__.py", line 73, in console_to_str
return s.decode(sys.__stdout__.encoding)
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 32: invalid start byte
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\basecommand.py", line 215, in main
status = self.run(options, args)
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\commands\install.py", line 335, in run
wb.build(autobuilding=True)
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\wheel.py", line 749, in build
self.requirement_set.prepare_files(self.finder)
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\req\req_set.py", line 380, in prepare_files
ignore_dependencies=self.ignore_dependencies))
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\req\req_set.py", line 634, in _prepare_file
abstract_dist.prep_for_dist()
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\req\req_set.py", line 129, in prep_for_dist
self.req_to_install.run_egg_info()
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\req\req_install.py", line 439, in run_egg_info
command_desc='python setup.py egg_info')
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\utils\__init__.py", line 676, in call_subprocess
line = console_to_str(proc.stdout.readline())
File "C:\DevApp\Anaconda3\envs\rqtest\lib\site-packages\pip\compat\__init__.py", line 75, in console_to_str
return s.decode('utf_8')
UnicodeDecodeError: 'utf-8' codec can't decode byte 0xb6 in position 32: invalid start byte