Python 3.x python 3中ImportLib的属性错误
我正在尝试动态导入特定的模块和方法。为了动态导入模块,我编写了CheckCode.py,它有一个类SystemConfigure和方法Snp_Configure。需要导入的模块是SnpBase.py,它依次具有类SnpBase和方法Unix\u Base\u Configure。为了动态导入模块和方法,我使用importlib功能。然而,当我做同样的事情时,我得到了AttributeError。有人能帮我找出缺了什么吗。谢谢 检查代码.pyPython 3.x python 3中ImportLib的属性错误,python-3.x,Python 3.x,我正在尝试动态导入特定的模块和方法。为了动态导入模块,我编写了CheckCode.py,它有一个类SystemConfigure和方法Snp_Configure。需要导入的模块是SnpBase.py,它依次具有类SnpBase和方法Unix\u Base\u Configure。为了动态导入模块和方法,我使用importlib功能。然而,当我做同样的事情时,我得到了AttributeError。有人能帮我找出缺了什么吗。谢谢 检查代码.py class SystemConfigure():
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)
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")
SnpBase.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)
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'
模块
SnpBase
没有名为Unix\u Base\u Configure
的函数
模块
SnpBase
有一个名为SnpBase
的类,该类有一个名为Unix\u Base\u Configure的函数
因此,错误是:
AttributeError: module 'SnpBase' has no attribute 'SnpBase.Unix_Base_Configure'
是正确的。我使用importlib import\u模块中的以下语句,我还使用以下命令调用CheckCode。m=SystemConfigure({'protocol':'Base','osname':'Unix','device':'dut'})