I have a string which contains a python code. Is there a way to create a python module object using the string without an additional file?
content = "import math\n\ndef f(x):\n return math.log(x)"
my_module = needed_function(content) # <- ???
print my_module.f(2) # prints 0.6931471805599453
Please, don't suggest using eval
or exec
. I need a python module object exactly. Thanks!