This project has moved. For the latest updates, please go here.

Breakpoint not working for module loaded using imp.load_module


Repro requires two modules, as follows:
import imp
import module1
with open('', 'r') as f:
    module1 = imp.load_module('module1', f, "", (".py", "r", imp.PY_SOURCE))
import sys
def foo():
Then set a breakpoint in at print on line #1, and then another one on print inside foo().

To repro, launch using "Start with Debugging" on the opened file outside of any project (this is crucial; if files are in an opened project, it will not repro). The first breakpoint - in global scope - is hit, but the one inside the function is not.

If 'import module1' line is removed from, then we hit both breakpoints correctly.

The issue here is that when the module is loaded first time using import, its code object has an absolute file path (resolved by Python). That path matches the one specified in the breakpoint, and so we mark the breakpoint as bound - meaning that any future matches against that breakpoint require an exact path match. However, when we then load the same module with load_module(), this creates a new code object with a relative file path (as specified in the call; Python does not try to normalize it). When that module is run, all code objects in it have co_filename as a relative path as well, and so it doesn't match against the bound breakpoint anymore.