• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

在终端启动Python时报错的解决方案

python 搞代码 4年前 (2022-01-09) 16次浏览 已收录 0个评论

最近,在终端启动Python时,报了一个错误:

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 0xa7 in position 2167: illegal <p>本文来源gao!%daima.com搞$代*!码9网(</p>multibyte sequence

原因是Python的终端历史文件中包含中文,但不能正确使用gbk解码。查看了Python历史文件(系统用户目录下的.python_history),其编码方式为“utf-8”,而”history.py”中读取历史文件时使用的编码方式为“gbk”,所以会报错。 

解决方法

在history.py中使用`for line in open(filename, ‘r’)`来打开文件并读取每一行,使用的是默认的编码方式。需要根据不同文件的编码方式传入相应的参数值。

1. 首先检测出要打开的文件的编码方式。

在类中定义一个私有方法_get_encoding,作用是检测文件的编码方式,并返回。(需要导入chardet包)

def _get_encoding(self, filename=None):
  	if filename is None:
  		return

  	with open(filename, 'rb') as f:
  		return chardet.detect(f.read())['encoding']

2. 修改历史文件内容的读取

encoding = self._get_encoding(filename)
      
for line in open(filename, 'r', encoding=encoding):
self.add_history(lineobj.ReadLineTextBuff(ensure_unicode(line.rstrip())))

以上就是在终端启动Python时报错的解决方案的详细内容,更多关于终端启动python报错的资料请关注搞代码其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:在终端启动Python时报错的解决方案

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址