2017-04-30 10 views
0

特定のモジュールとメソッドを動的にインポートしようとしています。モジュールを動的にインポートするには、クラスSystemConfigureとメソッドSnp_Configureを持つCheckCode.pyを作成しました。インポートが必要なモジュールはSnpBase.pyであり、SnpBaseクラスとUnix_Base_Configureメソッドがあります。モジュールとメソッドを動的にインポートするには、importlib機能を使用しています。しかし、同じことをするときに私はAttributeErrorを取得しています。何人かが私に何が欠けているかを理解するのを手伝ってもらえますか?ありがとう。属性Python 3のImportLibでエラーが発生しました

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' 
+0

私は次の文を使用しています from importlibインポートimport_module また、次のコマンドでCheckCodeを呼び出しています。 m = SystemConfigure({'プロトコル': 'Base'、 'osname': 'Unix'、 'device': 'dut'}) – user2905950

答えて

0

モジュールSnpBaseにはUnix_Base_Configureという機能はありません。

モジュールSnpBaseSnpBaseというクラスがあり、そのクラスは、そのエラーUnix_Base_Configure

と呼ばれる機能があります。

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

が正しいことを。

関連する問題