I have a project that tries to create a new module dynamically and then in a subsequent exec
statement tries to import that module.
import imp
s="""
class MyClass(object):
def __init__(self):
pass
def foo(self):
pass
"""
mod = imp.new_module("testmodule.testA")
exec s in mod.__dict__
exec "import testmodule.testA"
But this throws this exception:
Traceback (most recent call last):
File "test.py", line 14, in <module>
exec "import testmodule.testA"
File "<string>", line 1, in <module>
ImportError: No module named testmodule.testA
I've tried several things: adding it to sys.modules, creating a scope
dict containing the name and the module as well. But no dice. I can see testmodule.testA when I do a print locals()
in my exec
statement, but I can't import it. What am I missing here?
Thank you.