【发布时间】: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