Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python mode in Emacs: No such file or directory, pdb

I have a python script that I want to debug with python-mode. I read in this thread that I can debug my python script with M-x pdb, however I get the following error:

Searching for program: no such file or directory, pdb

I can provide python -m pdb my_source_file.py in the prompt in the minibuffer, but it would be nice if Emacs could infer this command directly from the file on which I run M-x pdb

Update:

Running on:

  • Red Hat Enterprise Linux Server release 5.1 (Tikanga)
  • Emacs 23.3.1

Differences between paths

I get different paths when I run M-: exec-path and when I run M-: (getenv "PATH") (the one returned by M-: (getenv "PATH") is longer).

With this:

  • Where is pdb located? How can I add it to the Emacs path?
  • Is there a way to ask Emacs to also look into the paths held by the environment variable PATH?
like image 676
Amelio Vazquez-Reina Avatar asked Feb 06 '12 21:02

Amelio Vazquez-Reina


People also ask

How do I run a pdb in Python?

Starting Python Debugger To start debugging within the program just insert import pdb, pdb. set_trace() commands. Run your script normally, and execution will stop where we have introduced a breakpoint. So basically we are hard coding a breakpoint on a line below where we call set_trace().

Does pdb come with Python?

pdb is part of Python's standard library, so it's always there and available for use. This can be a life saver if you need to debug code in an environment where you don't have access to the GUI debugger you're familiar with. The example code in this tutorial uses Python 3.6.

What does import pdb pdb Set_trace () do?

It's import pdb; pdb. set_trace() . When execution reaches this point in the program, the program stops and you're dropped into the pdb debugger. Effectively, this is the same as inserting a breakpoint on the line below where we call set_trace() .


2 Answers

Further to my comment earlier, and your subsequent update to the question:

First figure out a value for $PATH that works in your terminal. Use which pdb to find where the pdb executable is located.

Then, set the $PATH environment variable explicitly in Emacs, and sync it to exec-path as follows:

(setenv "PATH" "/usr/local/bin:/usr/bin:/bin:/some/other/dir")
(setq exec-path (split-string (getenv "PATH") path-separator))

It's possible you would need to also explicitly set PYTHONPATH or similar environment variables; you can do that using lines like the "setenv" line above, or just use the exec-path-from-shell elisp package.

Update

Okay, so it turns out Emacs' pdb command isn't provided by python-mode, and it expects to find an executable called "pdb". The easy way to fix this, then is to create a shell wrapper called "pdb", in a directory on your $PATH:

#!/bin/sh
exec python -m pdb "$@"

(I found a note here suggesting this technique.)

The equivalent under Windows would be a file called pdb.bat, containing:

python -u -m pdb %1

(The -u prevents Python from buffering its output.)

like image 81
sanityinc Avatar answered Oct 16 '22 21:10

sanityinc


To run the Python Debugger, M-x pdb expects to find an executable named pdb. While the pdb executable may exist in some Python distributions, it doesn't exist in all of them.

A proposal to fix this is in GNU bug report #21521: pdb default suggested command.

Until the bug is fixed, you can set the variable gud-pdb-command-name to define the command used to launch pdb. In .emacs, add...

(setq gud-pdb-command-name "python -m pdb")
like image 7
Chad Nouis Avatar answered Oct 16 '22 20:10

Chad Nouis