How can I get the reference to a module from another package, by name?
Considering:
genericCall(packageName, moduleName, methodName):
#Call moduleName.methodName
#knowing that the moduleName is in packageName
Where
packageName="p1"
moduleName="m1"
methodName="f1"
You'll have to import either module or package, and to do it by name, you can use __import__ or importlib.import_module.
import importlib
def genecirCall(package_name, module_name, function_name):
# import module you need
module = importlib.import_module('%s.%s' % (package_name, module_name))
getattr(module, function_name)() # make a call
Note, that in this example, the module variable will be local.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With