【问题标题】:attribute Error with ImportLib in python 3python 3中ImportLib的属性错误
【发布时间】:2017-06-14 21:06:17
【问题描述】:

我正在尝试动态导入特定的模块和方法。为了动态导入模块,我编写了 CheckCode.py,它有一个 SystemConfigure 类和 Snp_Configure 方法。需要导入的模块是 SnpBase.py,它又具有类 SnpBase 和方法 Unix_Base_Configure。为了动态导入模块和方法,我使用了 importlib 功能。但是我在做同样的事情时得到了 AttributeError 。有人可以帮我弄清楚缺少什么。谢谢。

CheckCode.py

class SystemConfigure():

    def __init__(self,snp_dict):
        print ("I am in Init of the SystemConfigure")
        SystemConfigure.Snp_Configure(self,snp_dict)

    def Snp_Configure(self,snp_dict):
        dict = snp_dict
        osname = dict['osname']
        protocol = dict['protocol']
        module = "Snp" + protocol
        func_string = module + "." +osname + "_" + protocol + "_" + "Configure"
        #func_string = osname + "_" + protocol + "_" + "Configure"
        print ("You have called the Class:", module, "and the function:", func_string)
       # my_method =getattr(import_module(module),"SnpBase.Unix_Base_Configure")
        mod = import_module(module)
        #return mod.SnpBase.Unix_Base_Configure(snp_dict)
        func = getattr(mod, func_string)
        func(snp_dict)

SnpBase.py

class SnpBase():

    def __init__(self,dict):
        pass
        print("BASE INIT")

    def Unix_Base_Configure(self,dict):
        print ("GOT IN THE UNIX BASE CLASS FUNCTION")

    def Linux_Base_Configure(self,dict):
        print("GOT IN THE LINUX BASE CLASS FUNCTION")

错误信息

  func = getattr(mod, func_string)
AttributeError: module 'SnpBase' has no attribute 'SnpBase.Unix_Base_Configure'

【问题讨论】:

  • 我正在使用 importlib import import_module 中的以下语句我还使用以下命令调用 CheckCode。 m = SystemConfigure({'protocol':'Base','osname':'Unix','device':'dut'})

标签: python-3.x


【解决方案1】:

模块SnpBase 没有名为Unix_Base_Configure 的函数。

模块SnpBase 有一个名为SnpBase 的类,该类有一个名为Unix_Base_Configure 的函数

所以错误:

AttributeError: module 'SnpBase' has no attribute 'SnpBase.Unix_Base_Configure'

是正确的。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-12-10
    • 2017-03-22
    • 2016-01-26
    • 2013-10-20
    • 2015-04-19
    • 2019-03-26
    • 1970-01-01
    • 2014-03-21
    相关资源
    最近更新 更多