Python,尝试实例化使用 __import__ 导入的类,得到“模块”;对象不可调用
做:
我一直在研究如何做到这一点,但我不知道我做错了什么,我想使用 import 导入一个类,然后实例化它,并这样 类,来自名为“action_1”的文件,我已经导入/附加了该路径)
class Action_1 ():
def __init__ (self):
pass
我如何尝试导入然后实例化它
imprtd_class = __import__('action_1', globals(), locals(), ['Action_1'], -1)
#instantiate the imported class:
inst_imprtd_class = imprtd_class()
>>>> '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 use 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
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
__import__
返回模块,而不是fromlist
中指定的任何内容。查看__import__
文档 并查看示例以下。注意,在上述情况下,fromlist 不是必需的,但如果
action_1
模块位于包内(例如mystuff.action_1
),则它是必需的。有关更多信息,请参阅 __import__ 文档。__import__
returns the module, not anything specified in thefromlist
. Check out the__import__
docs and see the example below.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.