6

I'm writing a python package and I want to use pdb to debug it. When I try to set break point in one of the files, I get an error:

The specified object 'CaptureManager.frame' is not a function or was not found along sys.path

I googled it, and found a solution:

append the directory which contains my file into sys.path

sys.path.append(os.path.join(os.getcwd(),"project_cameo"))

But after few times, I get very annoyed, because I have to do it every time I restart my debug session. Is there a 'smart' way of doing it?

Alex M
  • 2,756
  • 7
  • 29
  • 35
scott huang
  • 2,478
  • 4
  • 21
  • 36

2 Answers2

8

According to this answer you can also set a break point by writing the full path to filename (or path relative to directory on sys.path)

For example

b /path/to/module.py:34
> Breakpoint 1 at /path/to/module.py:34
berkelem
  • 2,005
  • 3
  • 18
  • 36
3

You have to load your module in order to use it (debug it in your case). Python looks at sys.path variable to load it's modules.

From the docs,

sys.path: A list of strings that specifies the search path for modules. Initialized from the environment variable PYTHONPATH, plus an installation-dependent default.

It is initialized from the PYTHONPATH environment variable; so you can add your path to this env variable instead of your module.

Or you can add the sys.path.append(os.path.join(os.getcwd(),"project_cameo")) line to your module at the top.

Chen A.
  • 10,140
  • 3
  • 42
  • 61
  • I will try change the PYTHONPATH to see if it's convenient. but I prefer not to change my source code for debugging. – scott huang Sep 20 '17 at 13:31
  • @scotthuang let me know if that helped – Chen A. Sep 20 '17 at 15:02
  • yes. It helps. I think the conclusion is that there is no 'native' way of doing this, but we can do it by modifying os.path. But I think there is a possibility that we will mess up the normal search path by doing that. It is ok for debugging, but it's something I need keep in mind when I do that. – scott huang Sep 23 '17 at 02:12
  • @scotthuang you can add the path to the end of the sys.path, and you will just extend it, won't mess anything. If you found my answer helpful, please consider upvoting it, thanks – Chen A. Sep 23 '17 at 07:46