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