Take a look at this other stackoverflow question. I think getting the current depth of the stack reliably distinguishes between execution contexts regardless of filename. It should automatically separate a wrapping call (shallow) from user-defined code (deep). From here, it says "The trace hook is modified by passing a callback function to sys.settrace()
." So I think you can filter out events when the depth <= 3. As soon as the depth is > 3, you can start tracing since the deep stack is user-defined functions.