Question
· Jan 13

How do you debug embedded python (Docker Container) in VSCode

Just curious what the process is for debugging either a .py file that is uploaded to a specific namespace/location or a python classmethod defined in a .cls file.

 

My method can be run via the "Debug" breadcrumb but it doesnt hit breakpoints it just runs without a debugger. Also objectscript methods debug correctly

Product version: IRIS 2024.1
$ZV: 2024.1
Discussion (3)5
Log in or sign up to continue

Hi John, 

As Raj suggested, we don't currently support debugging in vscode. However, if you are open to interactively debugging from terminal, here're some ideas.

1. If you have a specific part of code you're interested in, I'd recommend manually setting breakpoint() or import pdb; pdb.set_trace() in your python code. When hitting these lines, pdb will pause the execution of your Python code and drop you into an interactive debugging session. This allows you to inspect variables, evaluate expressions, and step through your code line by line to better understand its behavior and identify any issues. See here for how to use pdb commands.

Caveat 1: For the python code defined in an ObjectScript file with [ Language = python ], you won't be able to see python the source code when running list (which is a useful pdb command). This is due to Python linecache module only picking up regular files on your filesystem whereas [ Language = python ] stores the code in the ^ROUTINE global.

Caveat 2: If your Python method A calls into an objectscript method B, which calls into another Python method C, this will leave you at the "A" level of call stack. You can't cross the line between Python stack frame to ObjectScript stack frame using pdb. But you can move between adjacent Python stack frames.  

2. If you don't know where you want to start debugging and only want to inspect the state of python after it fails, you can try this internal API, (not guaranteed to work in the future)

Do $System.Python.Debugging(1)
Do ##class(YourClass).YourMethod()
Set pdb = $System.Python.Import("pdb")
Do pdb.pm()

This also drop you into an interactive pdb debugging session of the last error. You still have the caveats described above.

Just to clarify,

Do $System.Python.Debugging(1)

Will end up causing the C piece of SW that translates IRIS to python to call this:

void PyErr_PrintEx(int set_sys_last_vars)
 
If set_sys_last_vars is nonzero, the variable sys.last_exc is set to the printed exception.

In practice what this means, is somewhere close to your exception this gets called:

       if (PyErr_Occurred())
          PyErr_Print();

Which means you will actually be able to do a useful pdb post mortem
pdb.pm() or USER>d pdb.pm()

After your exception.