开发者

Python, trying to instantiate class imported using __import__, getting ''module' object is not callable'

I've been researching how to do this and I can't figure out what I am doing wrong, I want to u开发者_Go百科se import to import a class and then instantiate it and am doing it as so:

the class, from a file called "action_1", I have already imported / appended the path to this)

class Action_1 ():
    def __init__ (self):
        pass

how I am trying to import then instantiate it

imprtd_class = __import__('action_1', globals(), locals(), ['Action_1'], -1)

#instantiate the imported class:
inst_imprtd_class = imprtd_class()
>>>> 'module' object is not callable


__import__ returns the module, not anything specified in the fromlist. Check out the __import__ docs and see the example below.

>>> a1module = __import__('action_1', fromlist=['Action_1'])
>>> action1 = a1module.Action_1()
>>> print action1
<action_1.Action_1 instance at 0xb77b8a0c>

Note, the fromlist is not required in the above case, but if the action_1 module was within a package (e.g. mystuff.action_1) it is required. See the __import__ docs for more info.

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜