Assume you have module foo and object bar. Usually you simply import object from module by doing:
from foo import bar
this is simple and straightforward. I want to accomplish same thing programatically. Name of the object "bar" is provided by the user, and can be some arbitrary value, so I need something like:
eval("from foo import %s" % ("bar"))
I'd just like to get a way to accomplish this. For some reason:
eval("from string import lower")
gives me syntaxerror.
I'm aware of some security consideration here (someone may import something stupid etc, break stuff etc). For the time being we can leave security consideration aside. I just want to import object from module and use this object later. Assuming the module name is string and the object I need to get is function lower() I need something like this:
import imp
f, filename, rest = imp.find_module("string")
my_module = imp.load_module("string", f, filename, rest)
object_i_need = my_module.load_object_from_module("lower", my_module)
object_i_need("HALLO") # should return "hallo"
Third line is missing at the moment, there is no load_object_from_module function, or I haven't found it yet.
Any suggestions are welcome.