简体   繁体   中英

How do you debug imported Jupyter notebooks in VS Code?

Using the Python Extension for Visual Studio Code you can define code cells in your Python code using #%% :

#%%
a = 1
breakpoint()
b = 2

Then you can click "Run Cell" and VS Code will open a Python Interactive window and run the current cell. Unfortunately if you run the cell above it will fail because of the breakpoint() line. The error is:

StdinNotImplementedError: raw_input was called, but this frontend does not support input requests.

If you comment out the breakpoint it runs but doesn't stop at the breakpoint:

#%%
a = 1
#breakpoint()
b = 2

If you remove the special "Run Cell" comment it will stop at the breakpoint but then you lose Python Interactive:

a = 1
breakpoint()
b = 2

You can (kindof) get the best of both worlds by replacing the breakpoint by clicking in the gutter in Visual Studio Code to add a breakpoint:

显示断点

However I would prefer to write my breakpoints in code. Any ideas on supporting this case?

I've just found this underrated library xdbg . I've had some annoying exceptions running on Python 3.7, but it's working fine on my interactive mode in VS Code.

I think that xdbg is an interesting alternative other than running your imported Notebook using the classic Python debugger in VS Code . Note that when you are working in interactive mode, you are basically building a Python script, so you can use the debugger very simply. The only drawback is that every time you run the debugger the script starts from scratch (which can be annoying if you start your script loading a lot of data or so).

I'd love to have debugging options for the interactive Python console in VS Code, but it seems they have no support for it yet.

Hope this helps.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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