How to Debug Embedded Python?
Folks!
Could you please share any best practices on how to debug Embedded Python code?
Given I have the following class method:
ClassMethod HideShip() As %Status [ Language = python ]
{
import iris
shipsgl=iris.gref(iris.cls(__name__)._GetParameter("BoardStorage"))
shipsgl.kill()
from random import Random
board=iris.cls(__name__)._GetParameter("BoardRange")
x=random.randint(0,board+1)
y=random.randint(0,board+1)
shipsgl.set([x,y],1)
}
And when I run it shows the following error:
USER>d ##class(eshvarov.sample.SeaBattle.GamePython).HideShip() D ##CLASS(eshvarov.sample.SeaBattle.GamePython).HideShip() ^ <THROW> *%Exception.PythonException <THROW> 230 ^^0^D ##CLASS(eshvarov.sample.SeaBattle.GamePython).HideShip() <class 'KeyError'>: Invalid args -
Any idea how to guess in what line could be the problem?
The one-by-line debugger in VSCode doesn't help (I guess it's not implemented yet).
Comments
in my various examples I took these strategies:
- try to run the Py code in PY shell in a REAL terminal or docker command line console
- error messages directly from the shell are much more useful
- insert temporary print(...) statements at suspicious points
- what I see by COS <THROW> is often more obfuscation than a hint
to my experience, your 'KeyError' indicates that you try to get a value
from a non-existing global or subscript. in COS it would be <UNDEF>
Hi Evgeny,
You could consider using the Python "traceback" module.
For example:
Class TEST.TraceBack [ Abstract ]{ClassMethod GetErrorTrace() As %String [ Language = python ]{import tracebackerrStr=""try:
x = 7/0except:
errStr=traceback.format_exc()return errStr}}Demonstration:
USER>w ##class(TEST.TraceBack).GetErrorTrace() Traceback (most recent call last): File "TraceBack", line 6, in GetErrorTrace ZeroDivisionError: division by zero
Kind regards,
Alex
Thanks, @Alex Woodhead! Looks very interesting!
For debugging embedded python, i do not use ObjectScript wrapper.
Use "pure" python, with the VsCode and the irispython interpreter, after use the debugger like any normal python program/script :

@Guillaume Rongier I've had trouble getting this setup to work. Can you share your configuration (settings.json file?) to get VS Code to use irispython?