Is there a way to debug Jupyterlab .py code?

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