简体   繁体   English

vscode:在调试控制台中评估多个选定的行会导致缩进错误(使用 python 类)

[英]vscode: evaluating multiple selected lines in the debug console causes an indentation error (with python classes)

I have been struggling for a while with the problem that in VSCode I would like to evaluate multiple lines of Python code in the debug console without having to select each line individually and send it to the debug console.我一直在努力解决在 VSCode 中我想在调试控制台中评估多行 Python 代码而不必单独选择每一行并将其发送到调试控制台的问题。 However, when I select multiple lines at once and want to evaluate them in the debug console, the existing indentations are copied to the debug console and cause an indentation error there.但是,当我一次选择多行并想在调试控制台中评估它们时,现有的缩进会被复制到调试控制台并在那里导致缩进错误。 I have since been able to narrow down the problem to the point where I have determined that this occurs mainly when debugging class methods.从那以后,我能够将问题缩小到我确定这主要发生在调试类方法时的程度。 A minimal example is given below.下面给出了一个最小的例子。

class MyClass:
    def __init__(self):
        pass
    def run(self):
        a = 3
        b = 4
        c = a + b
MyClass().run()
  

If I set a breakpoint at line a = 3 , which is then reached during debugging, and I then select lines a = 3 up to and including c = a + b and want to have all 3 lines evaluated in the debug console, this results in the lines being copied to the debug console as follows:如果我在a = 3行设置断点,然后在调试期间到达,然后我选择行a = 3直到并包括c = a + b并希望在调试控制台中评估所有 3 行,结果在复制到调试控制台的行中,如下所示:

a = 3
        b = 4
        c = a + b

Which causes the following error:这会导致以下错误:

Traceback (most recent call last):
  File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 419, in evaluate_expression
    compiled = compile(_expression_to_evaluate(expression), '<string>', 'eval')
  File "<string>", line 1
    a = 3
      ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 1207, in internal_evaluate_expression_json
    pydevd_vars.evaluate_expression(py_db, frame, expression, is_exec=True)
  File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 371, in new_func
    return _run_with_unblock_threads(original_func, py_db, curr_thread, frame, expression, is_exec)
  File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 339, in _run_with_unblock_threads
    return _run_with_interrupt_thread(original_func, py_db, curr_thread, frame, expression, is_exec)
  File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 310, in _run_with_interrupt_thread
    return original_func(py_db, frame, expression, is_exec)
  File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 421, in evaluate_expression
    Exec(_expression_to_evaluate(expression), updated_globals, frame.f_locals)
  File "/home/lukas/.vscode/extensions/ms-python.python-2021.9.1230869389/pythonFiles/lib/python/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<string>", line 2
    b = 4
    ^
IndentationError: unexpected indent

This is a very annoying problem.这是一个非常烦人的问题。 Any solution would be greatly appreciated.任何解决方案将不胜感激。

You really have copied the indent from the second line.你真的复制了第二行的缩进。

You can press Alt + Shift when you copy from a to a+b to avoid this.您可以在从a复制到a+b时按Alt + Shift以避免这种情况。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM