Package imp deprecated

The package imp is deprecated in python since version 3.4 and will be removed. This is critical as the button reload of the modules strongly depends on this package. I didn’t succeed to replace it with the recommended importlib, but maybe somebody has already created something.

I have tried overriding the onReload method, it runs without error but it does not do what it is intended, the submodules are still not reloaded:

def onReload(self):
        """
        Override reload scripted module widget representation.
        """
        logging.debug("Reloading Dosimetry4D")
        submoduleNames=['Dosimetry4DLogic', 'Dosimetry4DTest', 'fit_values', 'PhysicalUnits']
        import importlib
        for submoduleName in submoduleNames:
            module = importlib.import_module(".".join(['Logic', submoduleName]))
            importlib.reload(module)
            
        if isinstance(self, ScriptedLoadableModuleWidget): # Mandatory for now, reloading the widget
            ScriptedLoadableModuleWidget.onReload(self)

I made it to work with the following:

import importlib
mod = importlib.import_module('Logic', __name__)
importlib.reload(mod)
__submoduleNames__=['Dosimetry4DLogic', 'Dosimetry4DTest', 'fit_values', 'PhysicalUnits']

# and then...
    def onReload(self):
        """
        Override reload scripted module widget representation.
        """
        logging.info("Reloading Dosimetry4D")
        importlib.reload(mod)
        for submoduleName in __submoduleNames__:
            mod1 = importlib.import_module('.'.join(['Logic',submoduleName]), __name__)
            importlib.reload(mod1)

        if isinstance(self, ScriptedLoadableModuleWidget):
             ScriptedLoadableModuleWidget.onReload(self)
1 Like