14

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?
Community
  • 1
  • 1
Amelio Vazquez-Reina
  • 91,494
  • 132
  • 359
  • 564
  • 1
    Do you have `pdb` in your path? Have you tried setting the value of the `pdb-path` variable? What result do you get if you evaluate `(py-guess-pdb-path)` in an Emacs Lisp buffer? – Luke Girvin Feb 06 '12 at 22:02
  • Thanks @LukeGirvin I tried `M-:` `(py-guess-pdb-path)` and I got `(Debugger entered-Lisp error: (void-function py-guess-pdb-path)` – Amelio Vazquez-Reina Feb 06 '12 at 22:26
  • 1
    Strange, are you running the latest version of python-mode? – Luke Girvin Feb 06 '12 at 23:25
  • @LukeGirvin Yes, the latest stable version: `6.0.4` – Amelio Vazquez-Reina Feb 06 '12 at 23:26
  • 1
    Try `M-: exec-path RET` and `M-: (getenv "PATH")` to see what `$PATH` Emacs is using. If you're on OSX, it likely won't match what you see in a terminal, so you might want to try [this trick](http://stackoverflow.com/questions/8606954/path-and-exec-path-set-but-emacs-does-not-find-executable/8609349#8609349). – sanityinc Feb 07 '12 at 09:27
  • Thanks @sanityinc. I run both commands, and I can see differences between the paths returned (the environment variable returns a longer list). I have updated my question accordingly – Amelio Vazquez-Reina Feb 07 '12 at 14:46
  • I presume you've resolved the issue by now. Want to accept my updated answer for this one? :-) – sanityinc May 14 '12 at 10:35

7 Answers7

12

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.)

Carson
  • 17,073
  • 19
  • 66
  • 87
sanityinc
  • 15,002
  • 2
  • 49
  • 43
  • Thanks @sanityinc. On my shell, `which pdb` returns nothing, `which python` returns `/sw/python-2.7.1/bin/python`. I know I have `pdb` installed because I can debug with `python -m pdb my_file.pdb`. Do you know where I can find `pdb` ? – Amelio Vazquez-Reina Feb 07 '12 at 20:38
  • Also, when you said that I should set the environment variable $PATH in Emacs explicitly, I am not sure I understand. Does Emacs has its own environment variables? (other than those the shell has?). Is Emacs supposed to inherit the environment variables from the shell? – Amelio Vazquez-Reina Feb 07 '12 at 20:42
  • Aha, right - so pdb is just a module. See my updated answer above; $PATH is normally the same in Emacs as in your shell, but I had misunderstood your problem, so please ignore that part of my advice. – sanityinc Feb 08 '12 at 11:30
  • I think when you say `string-split` you mean `split-string`. – joefromct Dec 18 '14 at 16:04
  • @sanityinc, I think another easier way to find which pdb to use is to use the following command: `python -c 'import pdb; print(pdb.__file__)'` – Sandy Chapman Feb 22 '15 at 23:32
7

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")
Chad Nouis
  • 6,861
  • 1
  • 27
  • 28
3

At a shell prompt type

which pdb

In Emacs, type M-x customize. Select Programming > Tools > Gud. Set the value of gud-pdb-command-name to the path returned by which pdb.

If your version of Emacs presents a different organization for the customize menu, you could also try

C-h v gud-pdb-command-name

Then click on the customize link, and set the path to pdb there.

Though the instructions above are different, I found this out by reading "Running pdb under emacs" .

unutbu
  • 842,883
  • 184
  • 1,785
  • 1,677
2

You can create a custom command like this:

;; PDB command line
(defun user-python-debug-buffer ()
  "Run python debugger on current buffer."
  (interactive)
  (setq command (format "python -u -m pdb %s " (file-name-nondirectory buffer-file-name)))
  (let ((command-with-args (read-string "Debug command: " command nil nil nil)))
    (pdb command-with-args)))
Simon Bergot
  • 10,378
  • 7
  • 39
  • 55
1

In Emacs 23.3.1 and presumably higher, yet another variation is to use the Emacs shell, Eshell (M-x eshell). Under Eshell, there's a pre-existing, Lisp-based definition of pdb. These Lisp functions work in Eshell just like ordinary shell commands.

So pdb "./manage.py runserver" will start a Django server, for instance.

flaneur
  • 66
  • 3
0

Everyone is going wild saying you gotta make a pdb file and make it an executable and then type ./pdb your_code.py. It is easier than that.

Be where you want to run the debugger from. Probably in your python file, maybe use M-x cd to get somewhere.

Then type: M-x pdb

It will prompt you with:

Run pdb (like this):

You want to make that look like:

Run pdb (like this): python -m pdb your_code.py

Sometimes if I want to run my code as a module.

Run pdb (like this): python -m pdb -m some_package.my_code

Then type help and go read this https://docs.python.org/3/library/pdb.html

nackjicholson
  • 4,557
  • 4
  • 37
  • 35
0

My answer builds on what @Chad Nouis mentioned. a link

However, I've added this to python-mode, everytime python-mode loads, it will set gud-pdb-command-name to "python -m pdb"

;; Set the PDB command
(add-hook 'python-mode-hook
       (lambda () (setq gud-pdb-command-name "python -m pdb")))