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('__init__.py')
]
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__:
                __all__.append(class_pair[0])

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:

jobs.py:post_purchase_orders() --> Here I can do step-by-step debugging
|- vendor1.py:post_purchase_order() --> This method isn't treated as a blackbox
   |- vendor1.py:login()
      |- browser.py:get_element_by_id() --> 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?

Answer

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, 
            module)

And that did the trick.