开发者

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)
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜