报错详情
一次在cmd中进入python交互式界面,输入“python”出现如下错误:
Warning:
This Python interpreter is in a conda environment, but the environment has
not been activated. Libraries may fail to load. To activate this environment
please see https://conda.io/activation
Type "help", "copyright", "credits" or "license" for more information.
Failed calling sys.__interactivehook__
Traceback (most recent call last):
File "C:\Anaconda3\lib\site.py", line 439, in register_readline
readline.read_history_file(history)
File "C:\Anaconda3\lib\site-packages\pyreadline\rlmain.py", line 165, in read_history_file
self.mode._history.read_history_file(filename)
File "C:\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 0x80 in position 2047: illegal multibyte sequence
经回想之前操作,应该是不小心在输入代码时输入了全角字符,引起错误。然后google相关解决方案,找到博客(猛戳这里查看),按其讲述方法成功解决问题,现将其转载以作留档。
解决方案
把博客原文粘上来:
以下方案任选其一,推荐方案2,终身解决:
- 找到python_history文件,将其中的中文记录删除。在我这,该文件存在于C:\Users\starxhong\python_history;
- 打开C:\Python\Python37\lib\site-packages\pyreadline\lineeditor\history.py,修改第82行增加utf-8编码:
for line in open(filename, 'r', encoding='utf-8'):
...
作者本人的history.py
文件目录不在C:\Python\Python37\lib\site-packages\pyreadline\lineeditor\history.py
中,在conda的虚拟环境下,具体地址是:C:\Anaconda3\envs\python3\Lib\site-packages\pyreadline\lineeditor
,读者可做参考。