Debug Python in Emacs with PDB and the intended interpreter

To debug a Python script in Emacs, run M-x pdb and give it a command that uses the interpreter for your project, followed by -m pdb and the script's path. Emacs's GUD interface sends commands to PDB and follows the current source line. Installing another debugging package is unnecessary for this basic route. Starting GUD

If a script imports successfully in a terminal but fails inside the debugger, compare the interpreters before changing the import. The virtual environment used by your terminal may differ from the executable Emacs starts.

Start with one calculation you can inspect

Save this fictional equipment-order calculation as order_total.py:

def order_total(unit_price, quantity):
    return unit_price + quantity


print(order_total(12, 3))

It prints 15, while three items at 12 each should total 36. The mistake is small enough to follow without application startup or external services.

Visit the file in Emacs and run M-x pdb. Replace the suggested command with the appropriate interpreter command for your machine, using full paths where needed. On a project with a Unix-style virtual environment, that can look like:

/home/you/equipment/.venv/bin/python -m pdb /home/you/equipment/order_total.py

On Windows, select the environment's Scripts/python.exe executable instead. Substitute actual paths; these examples do not create an environment. Avoid shell activation commands, environment-variable expansions, or wildcards in the GUD startup command. GUD reads a debugger command line, rather than starting an interactive login shell.

Stop inside the function and examine its values

In the PDB interaction buffer, enter:

b 2
c
p unit_price
p quantity
p unit_price + quantity
p unit_price * quantity
n

The breakpoint targets line 2, the return statement in the saved example. c continues to it, and the p commands inspect its arguments and the two candidate calculations. The expressions show 15 and 36 for this fixture. n then advances without stepping into a called function. If your installed PDB stops at a slightly different point, use l to list the current source before continuing. Python PDB commands

Emacs displays the relevant source file and marks the current execution line. The interaction buffer retains the debugger conversation, so you can compare the expression with the line that produced the wrong result. GUD debugger operation

Correct the source and start a clean run

Quit the debugger with q, change the function to multiply, and save:

def order_total(unit_price, quantity):
    return unit_price * quantity


print(order_total(12, 3))

Run the same interpreter and script again. The expected printed total is now 36. Restarting makes the edited source and the running process agree; changing a file does not rewrite code already loaded in a process.

For a real project, first reproduce the failure with the same inputs. Inspect the values immediately before the wrong result, rather than stepping through unrelated initialization.

When the session starts in the wrong place

At a PDB prompt, these expressions identify the process and its current directory:

p __import__('sys').executable
p __import__('os').getcwd()

Compare the executable with the one used for your successful terminal run. Compare the working directory with any relative data path the script opens. A correct interpreter does not make data/orders.csv resolve against the script's folder automatically.

Finish by rerunning the corrected script with the intended interpreter, directory, and original failing inputs. Keep the debugger command with the reproduction notes so the next run starts the same environment.

Keep reading