Skip to content

Python Reload

py
import sys
def rld(*args, **kwargs):
    if not args:
        return
    elements = list(filter(None, args))
    for modulename in sorted(sys.modules):
        module = sys.modules[modulename]
        modulepath = getattr(module, "__file__", "")
        kill = False
        for element in elements:
            if "path" in kwargs and element in modulepath:
                kill = True
                break
            if element in modulename:
                kill = True
                break
        if kill:
            del sys.modules[modulename]

Usage

py
rld("myPyModule")