2

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 withpdb.set_trace() orbreakpoint(). 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?

askedOct 11, 2024 at 15:49
swhalemwo's user avatar

1 Answer1

1

Try in your terminalpython -m pydoc pdb orthis link to see all commands and keys for pdb.Be sure toimport pdb at the beginning of the file.

Seems you would like to have a more featured debugger, eventually a guiversion - you may have such one if you install/uselsp-mode anddap-mode, a gui debugger very close in appearance and functionality to vscodepython debugger (based on debugpy).

Do not expect all your whishes to be fullfilled, not all debuggers are the same,due to language(s) particularity or other various reasons.

answeredOct 14, 2024 at 11:34
Ian's user avatar

Your Answer

Sign up orlog in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

By clicking “Post Your Answer”, you agree to ourterms of service and acknowledge you have read ourprivacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.