我一直在努力解决这个问题,在VSCode中,我想在调试控制台中评估多行Python代码,而不必单独选择每一行并将其发送到调试控制台中。但是,当我一次选择多行并希望在调试控制台中计算它们时,现有的缩进会被复制到调试控制台中,并在那里导致缩进错误。从那以后,我已经能够将问题缩小到我确定这主要发生在调试类方法时的程度。下面给出了一个最小的例子。
class MyClass:
def __init__(self):
pass
def run(self):
a = 3
b = 4
c = a + b
MyClass().run()
如果我在a = 3
行设置一个断点,然后在调试期间到达,然后我选择a = 3
到并包括c = a + b
行,并希望在调试控制台中评估所有3行,这导致行被复制到调试控制台中如下:
a = 3
b = 4
c = a + b
导致以下错误:
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
这是一个非常恼人的问题。如有任何解决办法,我将不胜感激。
您确实从第二行复制了缩进。
当你从a
复制到a+b
时,你可以按Alt+Shift来避免这种情况。