6

When running M-x pdb from Emacs, the current directory seems to be set to that of the directory of the script you are running, so

pdb ~/path/to/project/__main__.py

Would set the working directory to ~/path/to/project/. However, what if I need the working directory to be a different directory - say, one directory back? Is there a way to set the directory that pdb/gud uses as the "working directory"? At the first pdb prompt I can change the directory with os.chdir(), which works, but I would like to not have to do this every time. Thank you!

Update:

I have found that one potential solution would be to set gud-chdir-before-run (mentioned by @Nsukami_ below) to nil and then change to the desired directory with (cd "/path/to/directory") before running pdb. I tried doing this with the gud-mode-hook but the directory is changed too late. I have also, tried to do this with by advising pdb and gud-mode-hook, but don't yet have the lisp skills for this apparently...Is there a good way to call cd before gud-mode-hook/ before the dubugger starts?

4
  • 3
    Just for my information, what are you trying to achieve? Including the parent directory in the python path? Or maybe something else? There is a variable named gud-chdir-before-run not sure if what you're looking for.
    – Nsukami _
    Commented Nov 21, 2015 at 4:26
  • gud-chdir-before-run sounds like exactly what I need. I won't be able to try it until I get to work next week though. It is hard for me to explain better, because I don't quite understand yet what was causing the problem. What I know is, exceptions were being thrown only during debugging, and the way to fix it was to change the working directory to the parent directory of the __main__.py script. All the necessary paths were included in my PYTHONPATH though, as far as I could tell, and everything seemed to be properly imported - that was a problem you already helped me fix the other day ; )
    – elethan
    Commented Nov 21, 2015 at 4:40
  • having exactly the same issue, my dir containing python scripts is on my PATH but for some reason pdb doesn't see this, I want to run from the dir with my data in but I can't - its very irritating - the answer below is a terrible clunky solution, way too much typing..
    – bph
    Commented Feb 20, 2017 at 15:44
  • As to running M-x gdb within Emacs, I traced the default-directory to comint-exec-1 and then I worked backwards to set my default-directory from within gud-common-init. My goal was to set the working directory so that my preferred .gdbinit was taken into consideration when firing up the gdb executable. I had to modify gud-common-init to prompt me for my preferred working directory and set it accordingly -- of course, we know that it must also have a forward trailing slash for certain platforms ... However, I have no idea about python or pdb.
    – lawlist
    Commented Dec 5, 2017 at 5:12

2 Answers 2

1

Emacs specific:

Using a custom elisp function to call pdb with the -c COMMAND option, specifying the command as import os; os.chdir('..') (modifying directory as needed).

Working example:

(defun pdb-chdir ()
  """Change working directory when calling pdb"""
  (interactive)
  (let ((pdb-cmd-name
         (concat "python -m pdb -c \"import os; os.chdir('..')\" "
                 buffer-file-name)))
    (pdb pdb-cmd-name)))

Benefit being no additional scripts or files to create.


A couple options using pdb's .pdbrc files:

  • Add a .pdbrc file in the same directory as your .py script, containing
import os
os.chdir('/path/to/dir')

You would need to add just one .pdbrc file for each directory, but would apply when running pdb on any script in that directory

  • Add a .pdbrc to your home directory ~(this would be overridden by commands in a local .pdbrc file as above, if they both exist).
import os
os.chdir('..')  # one directory up relative to script

Only need to create one .pdbrc file, in the event you always want the working directory in the same relative path when using pdb. But note this would get executed every time you run pdb, regardless of script location.

0
import os; os.chdir('/path/to/dir');

To get know current working directory:

import os; os.getcwd();
1
  • Thanks for your answer. I mention this solution in my original question, but I was looking for a solution in Emacs that did not involve adding any Python code to existing scripts or commands at the prompt.
    – elethan
    Commented Oct 10, 2016 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.