I'm working on a main class that should programmatically load a class file and create an instance of it so it can be used in the rest of the main class. But I can't get it to work and can't find a right solution anywhere.
What seems to work at first sight is this:
from class import class
var = "classname"
obj = globals()[var]()
print(obj.message)
But then I have to import all the class files in that main class, which is something I'd rather avoid.
I tried other solutions with getattr and importlib but those things don't work. Example:
import importlib
importlib.import_module(var)
obj = var()
Edit: I tried this Python dynamic instantiation from string name of a class in dynamically imported module, but it's not a solution