Debug dynamically loaded modules in VSCode

I load some modules and import classes dynamically

modules = [ f
    for f in glob.glob(join(dirname(__file__), "*.py"))
    if isfile(f) and not f.endswith('')
for module in modules:
    with open(module, 'rb') as fp:
        module_name = splitext(basename(module))[0]
        ma = imp.load_module(
            'app.purchase.models.vendors.' + module_name, 
            fp, basename(module), ('.py', 'r', imp.PY_SOURCE))
        classes = { c for c in ma.__dict__.items() 
            if isinstance(c[1], type) and issubclass(c[1], PurchaseOrderVendorBase) }
        for class_pair in classes:
            setattr(self, class_pair[0], class_pair[1])
            if class_pair[0] not in __all__:

If I later call method from imported this way class it executes without problems. But step-by-step debugging in VSCode doesn’t step into the method as its source were unknown to it.

It stops though in some other method down in the call stack. But intermediate method isn’t shown in the call stack. So I have a call stack: --> Here I can do step-by-step debugging
|- --> This method isn't treated as a blackbox
      |- --> Here step-by-step debugging resumes

but in VSCode I see only this:

enter image description here

How do I tie the module to its source code file?


Ok, I used advice of rioV8 and modified module load logic. Instead of

        ma = imp.load_module(
            'app.purchase.models.vendors.' + module_name, 
            fp, basename(module), ('.py', 'r', imp.PY_SOURCE))

I’ve done:

        ma = imp.load_source(
            __name__ + '.' + module_name, 

And that did the trick.