Home > Blockchain >  Error when debugging a jupyter notebook in VSCode when a breakpoint is inside a while loop
Error when debugging a jupyter notebook in VSCode when a breakpoint is inside a while loop

Time:03-29

My problem is that debugging jupyter notebooks in VSCode does not work properly when I put a breakpoint inside a while loop.

Here is a working example :

i = 1
while i < 5:
    i =1

Putting a breakpoint at the last line works when this code is in a python file, but not when this code is in a jupyter notebook cell. More precisely, I can put a breakpoint, but when debugging, it does not stop at the break point. There is also an error issued in the terminal (I replaced my real name by myname):

Traceback (most recent call last):
  File "/home/myname/anaconda3/lib/python3.9/runpy.py", line 197, in _run_module_as_main
    return _run_code(code, main_globals, None,
  File "/home/myname/anaconda3/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/myname/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/__main__.py", line 45, in <module>
    cli.main()
  File "/home/myname/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 444, in main
    run()
  File "/home/myname/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/../debugpy/server/cli.py", line 285, in run_file
    runpy.run_path(target_as_str, run_name=compat.force_str("__main__"))
  File "/home/myname/anaconda3/lib/python3.9/runpy.py", line 268, in run_path
    return _run_module_code(code, init_globals, run_name,
  File "/home/myname/anaconda3/lib/python3.9/runpy.py", line 97, in _run_module_code
    _run_code(code, mod_globals, init_globals,
  File "/home/myname/anaconda3/lib/python3.9/runpy.py", line 87, in _run_code
    exec(code, run_globals)
  File "/home/myname/Documents/myname/experiments/jupyter-nb/como_logger_test.ipynb", line 72, in <module>
    "scrolled": true
NameError: name 'true' is not defined
(base) myname@Mallow:~/Documents/myname/experiments/jupyter-nb$  cd /home/myname/Documents/myname/experiments/jupyter-nb ; /usr/bin/env /home/myname/anaconda3/bin/python /home/myname/.vscode/extensions/ms-python.python-2022.2.1924087327/pythonFiles/lib/python/debugpy/launcher 37663 -- /home/myname/Documents/myname/experiments/jupyter-nb/test.ipynb

I did not find anything relevant to this issue when looking for vscode debugging "while loop" jupyter notebook error in google.

CodePudding user response:

Debug a Jupyter Notebook:

There are two different ways to debug a Jupyter notebook: a simpler mode called "Run by Line", and full debugging mode.

Run by Line Run by Line lets you execute a cell one line at a time, without being distracted by other VS Code debug features.

enter image description here

Debug Cell If you want to use the full set of debugging features supported in VS Code, such as breakpoints and the ability to step in to other cells and modules, you can use the full VS Code debugger.

enter image description here

The docs for all of the above are here: https://code.visualstudio.com/docs/datascience/jupyter-notebooks

CodePudding user response:

Currently, to debug the Jupiter notebook, you need to export it as a python file. After exporting to a python file, the visual studio code debugger allows you to step through code, set breakpoints, check status, and analyze problems. Using the debugger is a useful way to find and correct problems in notebook code.

  • Related