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!


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?

  • 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 _
    Nov 21 '15 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
    Nov 21 '15 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
    Feb 20 '17 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
    Dec 5 '17 at 5:12
import os; os.chdir('/path/to/dir');

To get know current working directory:

import os; os.getcwd();
  • 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
    Oct 10 '16 at 15:28

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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