import module by name
Posted: Fri Mar 26, 2021 3:22 pm
Hello! I'm working on porting some python code to micropython and I'm stuck on a certain point. I'd like to import a module by its name as string.
In python :
So there's no obvious import_module equivalent in upy that I've found. Do you have any ideas for functions I can investigate to do the same? My goal in importing the module by name was to instantiate a class in the module afterwards (and to prevent needless imports).
There are holes in my knowledge so if my reasoning is wrong, please let me know. I'm here to learn and eventually to share what I've learned.
In python :
Code: Select all
module = import_module(moduleName)
Code: Select all
class_ = getattr(module, className)
instance = class_()