Running a loop/multiple lines in vs code python debug console

Question:

How do I run a simple loop in VS Code’s python debug console? When I try to enter the following:

for el in dataset:

It gives me the error below. I seem to be able to enter variable names, but not multi-line commands like I can in the normal python REPL.

Traceback (most recent call last):
  File "/home/tensorflow/.local/lib/python3.6/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 416, in evaluate_expression
    compiled = compile(_expression_to_evaluate(expression), '<string>', 'eval')
  File "<string>", line 1
    for el in dataset:
      ^
SyntaxError: invalid syntax

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
  File "/home/tensorflow/.local/lib/python3.6/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_comm.py", line 969, in internal_evaluate_expression_json
    pydevd_vars.evaluate_expression(py_db, frame, expression, is_exec=True)
  File "/home/tensorflow/.local/lib/python3.6/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 368, in new_func
    return _run_with_unblock_threads(original_func, py_db, curr_thread, frame, expression, is_exec)
  File "/home/tensorflow/.local/lib/python3.6/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 336, in _run_with_unblock_threads
    return _run_with_interrupt_thread(original_func, py_db, curr_thread, frame, expression, is_exec)
  File "/home/tensorflow/.local/lib/python3.6/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 307, in _run_with_interrupt_thread
    return original_func(py_db, frame, expression, is_exec)
  File "/home/tensorflow/.local/lib/python3.6/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_vars.py", line 418, in evaluate_expression
    Exec(_expression_to_evaluate(expression), updated_globals, frame.f_locals)
  File "/home/tensorflow/.local/lib/python3.6/site-packages/debugpy/_vendored/pydevd/_pydevd_bundle/pydevd_exec2.py", line 3, in Exec
    exec(exp, global_vars, local_vars)
  File "<string>", line 1
    for el in dataset:
                     ^
SyntaxError: unexpected EOF while parsing
Asked By: xdhmoore

||

Answers:

You have 2 options:

  1. Write the command in a new editor window, then simply copy and paste the code in the debug console and press Enter
  2. Write the command directly in the debug console. When you want to enter a new line, press Shift+Enter. When the command is complete, execute with Enter
Answered By: GZZ

Another option:

2.1. Write the command directly in the debug console. Type {} at the end of current line, then press shift+enter. After complete writing all lines, press enter to execute.

for i in [1,2,3]: {'''press shift+enter here'''}
for i in [1,2,3]: {
    print(i)  #press enter here
}

Reference
https://youtu.be/p8zMXKFlkqk?t=50

Answered By: Vincent

I found this reference https://youtu.be/p8zMXKFlkqk?t=50 very helpful in running a function on variables existing in debug mode

Answered By: Voljum