启动运行python3时 UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 170: illegal multibyte sequence
重现
在cmd中输入Python,运行后,出现以下错误:
Python 3.7.3 (default, Mar 27 2019, 17:13:21) [MSC v.1915 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
Failed calling sys.__interactivehook__
Traceback (most recent call last):
File "C:\ProgramData\Anaconda3\lib\site.py", line 439, in register_readline
readline.read_history_file(history)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyreadline\rlmain.py", line 165, in read_history_file
self.mode._history.read_history_file(filename)
File "C:\ProgramData\Anaconda3\lib\site-packages\pyreadline\lineeditor\history.py", line 82, in read_history_file
for line in open(filename, 'r'):
UnicodeDecodeError: 'gbk' codec can't decode byte 0xa2 in position 170: illegal multibyte sequence
原因是在读取filename
的时候,是以unicode来读取的,需要打开C:\ProgramData\Anaconda3\lib\site-packages\pyreadline\lineeditor\history.py
这个文件。
打开history.py
,并编辑:
假如encoding参数即可。