欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

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

程序员文章站 2022-03-03 07:53:35
...

使用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('@[email protected]', '\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('@[email protected]', '\n'))
    else:
        expression = str(expression.replace('@[email protected]', '\n'))

保存退出,重启pycharm,便可完美兼容中文。