使用pycharm在debug python 2时,无法在debug console中输入中文,输入中文便会提示如下错误:
UnicodeEncodeError: 'ascii' codec can't encode characters in position 1-2: ordinal not in range(128)
直接导致带有中文的代码在进行debug时极不方便,解决方法如下:
\AppData\Local\JetBrains\Toolbox\apps\PyCharm-C\ch-0\(版本号)\helpers\pydev\pydevconsole.py
将以上文件中的代码片段
def console_exec(thread_id, frame_id, expression, dbg):
"""returns 'False' in case expression is partially correct
"""
frame = pydevd_vars.find_frame(thread_id, frame_id)
expression = str(expression.replace('@LINE@', '\n'))
改为
def console_exec(thread_id, frame_id, expression, dbg):
"""returns 'False' in case expression is partially correct
"""
frame = pydevd_vars.find_frame(thread_id, frame_id)
if isinstance(expression, unicode):
expression = unicode(expression.replace('@LINE@', '\n'))
else:
expression = str(expression.replace('@LINE@', '\n'))
保存退出,重启pycharm,便可完美兼容中文。