stringimporter
lets you import raw string as real Python modules
You can read the tests (in tests directory) or copy the following code.
import stringimporter
module_from_thin_air = """
def multiply_by_2():
return lambda x: 2*x"""
loader, mymodule = stringimporter.import_str('yihaaa', module_from_thin_air)
hello_2 = mymodule.multiply_by_2()("hello ")
None
Python 3. I don't know (yet?) how to emulate importlib.abc.SourceLoader on Python 2.7.