Windows系统启动python报文件编码错误

问题描述

在anaconda环境中启动python,出现如下错误:

(base) D:\>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 "D:\ProgramData\Anaconda3\lib\site.py", line 439, in register_readline
    readline.read_history_file(history)
  File "D:\ProgramData\Anaconda3\lib\site-packages\pyreadline\rlmain.py", line 165, in read_history_file
    self.mode._history.read_history_file(filename)
  File "D:\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 0x8a in position 1648: illegal multibyte sequence

分析

UnicodeDecoderError的错误可知,是Unicode解码错误。根据上面错误定位可知在open()函数中增加编码方式即可。

解决

找到如下位置文件的第82行:

"D:\ProgramData\Anaconda3\lib\site-packages\pyreadline\lineeditor\history.py"
for line in open(filename, 'r'):
# 增加编码方式
for line in open(filename, 'r', encoding='utf-8'):

再次尝试:

(base) D:\>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.
>>>

问题解决!

你可能感兴趣的:(Python)