I need to dynamically import a module and create a class.
This is my working code in Python 3.2:
klass = {}
mod = __import__('sites.' + self.className + '_login', fromlist=[self.className])
klass[self.className] = getattr(mod, self.className)
klass[self.className](**self.args)
The module is inside the sites
folder. It's called my_site_login
and the class within that module my_site
.
Since I upgrade to Python 3.3, the code stopped working. I read that _____import_____
was replace by importlib.import_module
. I tried several ways to do it but I can't get it to work.
This is what I tried:
https://stackoverflow.com/a/8790051/1095101
mod = getattr(import_module('sites.' + self.className + '_login'), self.className)
I can't remember what else I've tried. What I can say, is that none of the print()
I put right after any import attempt, was showing. I don't get any error message. It just does nothing.