Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Debugging python programs in emacs

People also ask

Can you debug in Emacs?

You can debug a C or C++ program using GDB, the GNU debugger, which was developed by the same organization that released Emacs. You can easily integrate it with Emacs to interactively debug programs. While you can use it from the Unix prompt, it has additional functionality when you use it within the Emacs editor.

How do I run a Python program in Emacs?

Once you open your python file in Emacs, you will need to start the python process with: M-x run-python or C-c C-p , which creates an inferior python shell buffer. This buffer will be created by a horizontal split, and the active buffer will be the one containing the python file.


Type M-x cd to change directory to the location of the program you wish to debug. Type M-x pdb. You'll be prompted with Run pdb (like this): pdb. Enter the name of the program (e.g. test.py).

At the (Pdb) prompt, type help to learn about how to use pdb.

Alternatively, you can put

import pdb 
pdb.set_trace()

right inside your program (e.g. test.py). Now type M-x shell to get a shell prompt. When you run your program, you'll be dumped into pdb at the point where pdb.set_trace() is executed.


For me, I needed to replace the default "pdb" with

python -m pdb myscript.py

The realgud package (available from MELPA) supports PDB (among a gazillion other debuggers), and has a host of neat features that Emac's PDB doesn't have.

The one I like best is the shortkeys mode. Once you start debugging a program, you can press n, s, c etc. right in the source window, instead of having to type these commands in the PDB buffer. It also supports Visual-Studio style keybindings with function keys (f10, f11, f5, etc).

After installing RealGUD, you need to run M-x load-feature realgud to load it, and you can start pdb with M-x realgud:pdb.