Is there a way to debug Jupyterlab .py code?

I can’t seem to a find a way to debug and step through the back-end code run by kernaes. Anyone have anyways stepping through and seeing what’s in the variables in the .py code while it’s being run by the kernel?

Hi Dylan,

Assuming you are looking to debug a Python script ending in .py that you are triggering to run via %run in a JupyterLab cell:

My favorite legit way instead of just putting print() in the script is to just put from IPython import embed; embed() into the code at a point you want to trigger dropping into an interactive prompt where you can query variables. The prompt you drop into is IPython based so if you set a variable my_var earlier you can just type my_var to see its state. No need for print(my_var).

(Aside: Using print() as a debugging step will be easier soon with Python 3.8 that adds the = specifier to the f-string syntax, see here.)

JupyterLab also has the terminal available so you could just use that and your standard debugging routes too. For example, you can use the terminal to launch IPython and then run some .py code with %run and then when an error is encountered, type %debug to do a post-mortem analysis. There are a lot more possibilities via the terminal but I won’t go into them since they are the more traditional route and you are more likely to find plenty of examples with a little searching on the internet. (pysnooper looks interesting and actively being developed.)

If you are talking about just running Python code in a JupyterLab cell and debugging that, then you have even more possibilities. See here or here. I haven’t tried it yet but there is even this fancy visual debugger, although it looks like it only works in the classic notebook interface and not JupyterLab.
In the future debugging code directly in the notebook could be a lot more interactive with an in-development debugger extension for JupyterLab, see the demo animation in this tweet by @jtp.

1 Like

Perfect thanks so much! Do you have an suggestions for debugging the front-end aspect? I’m using Jupyter Lab for a research project at university of british columbia and we’re having an issue debugging through the front-end. They decided to do TS with a Vue components in the frontend, but because of how jupyter lab interface is setup Vue debugger for chrome does not detect Vue components in the browser. Any suggestions?

I cannot offer anything for that.
Given the title of this thread, I’d suggest starting a new post for that question. That will give it better visibility.

1 Like

Python has a debugger , which is available as a module called pdb . It supports setting conditional breakpoints , stepping through the source code one line at a time, stack inspection, and more.

import pdb
msg = "this is a test"
pdb.set_trace()
print(msg)

Insert pdb.set_trace() anywhere and it will function as a breakpoint . When you execute the script by python test.py, you will in the debug mode.

1 Like

A new JupyterLab debugger extension has been released https://blog.jupyter.org/a-visual-debugger-for-jupyter-914e61716559. You might want to check it out :wink:

3 Likes

You can do simultaneous debugging of the Typescript and Python code of a Jupyterlab extension using vscode. WIth breakpoints and everything!

It does require some config files to set this up. Said config can be supplied by the cookiecutter at https://github.com/jupyterlab/debug-config-cookiecutter