I have a text string, code, containing the complete code for a python module. I know I could write the code to a local file and import it as cm with the following:
with open("codemod.py","w") as f:
f.write(code)
import codemod as cm
Is there a way do this directly from the code string without creating a local file? Perhaps using importlib
or __import()__
or exec()
?
I know that exec(code)
has the same effect as import codemod
, but how do I mimic the import as cm
option?