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

后端 未结 7 1611
南旧
南旧 2020-12-14 11:30

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

相关标签:
7条回答
  • 2020-12-14 11:32

    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

    0 讨论(0)
  • 2020-12-14 11:34

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

    0 讨论(0)
  • 2020-12-14 11:41

    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)))
    
    0 讨论(0)
  • 2020-12-14 11:41

    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.

    0 讨论(0)
  • 2020-12-14 11:46

    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")
    
    0 讨论(0)
  • 2020-12-14 11:49

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

    0 讨论(0)
提交回复
热议问题