扩展Python控制台实现中文反馈信息
"中文编程"知乎专栏原文地址 参考了周蟒的实现, 运行效果如下: $ python3 解释器.py Python 3.6.5 (v3.6.5:f59c0932b4, Mar 28 2018, 05:52:31) [GCC 4.2.1 Compatible Apple LLVM 6.0 (clang-600.0.57)] on darwin Type "help", "copyright", "credits" or "license" for more information. (ZhPyConsole) >>> 学 Traceback (most recent call last): File "<console>", line 1, in <module> 命名错误: 命名'学'未定义 >>> for i in range(1,4): ... 生 ... Traceback (most recent call last): File "<console>", line 2, in <module> 命名错误: 命名'生'未定义 完整源码在: 解释器.py 相关源码如下, 即改写InteractiveInterpreter.showtraceback方法. 仅为演示之用, 直接用了字符串替换.: def showtraceback(self): sys.last_type, sys.last_value, last_tb = ei = sys.exc_info() sys.last_traceback = last_tb try: 行 = traceback.format_exception(ei[0], ei[1], last_tb.tb_next) 汉化行 = [] if sys.excepthook is sys.__excepthook__: for 某行 in 行: for 英文 in self.字典: 某行 = 某行.replace(英文, self.字典[英文]) 汉化行.append(某行) self.write(''.join(汉化行)) else: # If someone has set sys.excepthook, we let that take precedence # over self.write sys.excepthook(ei[0], ei[1], last_tb) finally: last_tb = ei = None 定制sys.excepthook貌似更为合适, 还需研究如何实现. 参考资料 周蟒-zhpy 2019-02-10