Python交互式界面,启动python报“UnicodeDecodeError”错误

Python交互式界面,启动python报“UnicodeDecodeError”错误

报错详情

一次在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,读者可做参考。

Reference

博客

  • 6
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 3
    评论
评论 3
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值