Loading modules at runtime from functions
I would like to load modules at runtime.
If I do it like this it works:
a = __import__('datetime',globals(),locals(),[],-1)
for e in a.__dict__:
if not e.startswith("__"):
globals()[e] = a.__dict__[e]
But if I try to do this it doesn't work:
def module_loader(modname,g,l):
a = __import__(modname,g(),l(),[],-1)
for e in a.__d开发者_如何学Cict__:
if not e.startswith("__"):
g()[e] = a.__dict__[e]
module_loader('datetime',globals,locals)
Any help?
Your snippet above works for me if I call it as
module_loader('datetime', globals, locals)
def module_loader(modname,g,l):
a = __import__(modname,g(),l(),[],-1)
for e in a.__dict__:
if not e.startswith("__"):
g()[e] = a.__dict__[e]
module_loader('datetime', globals, locals)
精彩评论