Add and remove breakpoints without editing source files
Although you can add breakpoints by adding breakpoint()
or set_trace()
statements to your code, there are two issues with this approach:
- Firstly, once you have started running your code, there is no way to remove your breakpoint. I often find that once I start running my code and reach an initial breakpoint, I want to place another one and remove the initial breakpoint. After
breakpoint()
drops me into the debugger I can add additional breakpoints, but I can't remove the initial one. Although this can be mitigated somewhat by putting the initial breakpoint
statement higher up, if you have parametrised tests then even that is limited. I may find myself repeating cont
very often.
- Secondly, it requires changes to the source code. You need to remember to remove all
breakpoint()
commands before committing any code to version control, you have to remove them before switching branches, etc. I sometimes find I want to use the debugger to compare test runs between two branches, and having to edit the source code to add a breakpoint every time makes that a considerably slower and more error-prone exercise. I may even want to add a breakpoint in a library I'm calling, in which case the file I'm editing may not even me in my git repository but somewhere deep in my conda environment, increasing the risk of forgetting to remove it. Editing files to add break points is, in my humble opinion, ugly.
To add and remove breakpoints interactively without editing any source files, you can evoke pytest
as follows (in the bash shell):
python -mipdb $(type -p pytest) -s test_fileset.py
The -s
flag is crucial here, because it stops pytest from messing with stdin and stdout, and when running inside the debugger, pytest will fail to mess with stdin and stdout and everything will go wrong. The exact calling syntax will be different for different shells.