1

For a number of reasons - the main one being that Emacs is amazing! - I am trying to do all my Python development at work in Emacs instead of Pycharm, which I was using previously.

The one component I have not been able to get running right in Emacs is the debugger. I have been trying to use pdb in emacs to debug the application I am working on, but I haven't been able to figure out how to get pdb to find all the necessary modules. Instead, when I run my __main__.py file through pdb, I get a bunch of import errors because it can't find the modules for the project. If I manually add the appropriate paths to sys.paths and then import the modules one by one from the interactive pdb prompt, this works, but obviously is not the right solution.

In the Pycharm debugger there are two options: "add content roots to PYTHONPATH" and "add source roots to PYTHONPATH", which configure this stuff automatically. My issue is that I haven't figured out how to configure equivalent options in Emacs - i.e., I am not sure how/where to set PYTHONPATH so that pdb within Emacs recognizes it. I have tried setting it a few different was in my Emacs config, but nothing seems to work. Any suggestions? Thank you!

2
  • 1
    What about (setenv "PYTHONPATH" "/the/python/path") in your init file?
    – Nsukami _
    Nov 18, 2015 at 17:08
  • That did it actually! This was one of the first solutions that I tried, but it turned out I had entered the wrong path! If you put this as an answer I will accept it as the correct one.
    – elethan
    Nov 18, 2015 at 17:28

1 Answer 1

1

I am not sure how/where to set PYTHONPATH

To inform Emacs about your PYTHONPATH, just edit your init file and add this line:

(setenv "PYTHONPATH" "/the/python/path")

3
  • Could you please elaborate? Where can I find my python path, do I need to restart emacs before setenv starts working? I have tried a couple of paths already (from which python in the terminal or from just importing modules in an interactive shell and getting part of the module address, but I've had no success so far) Apr 30, 2016 at 5:36
  • @LeoAzevedo The value returned by which python is not your PYTHONPATH. Open a terminal, and type echo $PYTHONPATH. Replace "/the/python/path" in my answer, by the printed value. Let me know.
    – Nsukami _
    Apr 30, 2016 at 11:43
  • echo $PYTHONPATH returns nothing literally in my Terminal. However, which python returns Users/user/opt/anaconda3/bin/python3 May 30, 2021 at 6:29

Your Answer

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

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