I've mostly used R with ESS before, but now I'm using python for a data sciency project. My general workflow is having a split screen with code on one side and the shell on the other (I generally don't run entire scripts when developing code as parts of my code can easily take too long a time to re-run it over and over again). I've now written some code, and have to debug it. I'm using pdb, and can enter the debugging environment with pdb.set_trace()
or breakpoint()
. But coming from R, some aspects of the debugging experience surprise me, two in particular:
First, when in debug mode, I cannot use python-shell-send-statement to evaluated the current line. E.g. executing this function:
def foo (bar):
breakpoint()
baz = bar + 10
return(baz)
will place an arrow (=>) at the "baz = bar +10" line and move the cursor there, but if I then run C-c C-e (python-shell-send-statement) I get a long error message.
Traceback (most recent call last):
File "/usr/lib/python3.12/cmd.py", line 214, in onecmd
func = getattr(self, 'do_' + cmd)
^^^^^^^^^^^^^^^^^^^^^^^^^^
AttributeError: 'Pdb' object has no attribute 'do___PYTHON_EL_eval'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<string>", line 17, in __PYTHON_EL_eval
File "/home/me/test.py", line 26, in <module>
breakpoint()
^^^
NameError: name 'bar' is not defined
I can access the values in the shell frame, but this is much less convenient for me, in particular because in R I've developed the habit of writing my code to a large extent in debug mode as it allows a quick cleanup of function-specific variables. Now while in debug mode, I cannot quickly change something in the code (e.g. modify the line to baz = bar + 5) and evaluate the new function (i can manually copy the modified line to the shell buffer and evaluate it there, but this is not convenient, especially as snippets get larger). Is this some configuration issue on my side or just a fundamental aspect of python debugging?
Secondly, I couldn't find any navigation keybindings once in debug mode. I write n or s in the shell window to step through the program, but that's two separate keypresses (n or s and then return), whereas I would have expected there to be some emacs keybinding for debugger interaction (e.g. the ESS debugger has M-N, M-U and M-Q for next, up and quit, respectively). when searching via C-h f I also cannot find any elisp functions that call the python debugging functions. I'm wondering, do these exist, or does the emacs python debugging workflow consist of using the pdb commands?