pdb: set a breakpoint on file which isn't in sys.path

Viewed 4131

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?

2 Answers

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

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.

Related