当前位置: 首页 > 工具软件 > debugConsole > 使用案例 >

解决pycharm在debug console中输入中文UnicodeEncodeError的问题

公良奇
2023-12-01

使用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,便可完美兼容中文。

 类似资料: