我不能用這個import foomethod_to_call = getattr(foo, 'bar')result = method_to_call()因為模塊名稱是硬編碼的,我不能使用它module = __import__('foo')func = getattr(module, 'bar')func()因為模塊是嵌套的。我試過這個customer = 'jci'module = __import__('customer.{customer_name}.gt'.format(customer_name=customer_name)) # AttributeError: module 'customer' has no attribute 'get_gt'#module = __import__('customer.{customer_name}'.format(customer_name=customer_name), fromlist=['gt']) # AttributeError: module 'customer.jci' has no attribute 'get_gt'#module = __import__('customer.{customer_name}.gt'.format(customer_name=customer_name), fromlist=[]) # AttributeError: module 'customer' has no attribute 'get_gt'func = getattr(module, 'get_gt')gt = func() 但因錯誤而失敗,在注釋中與每個變體一起指定。get_gt()是目錄內(nèi)gt.py文件內(nèi)的函數(shù)customer/jci。每個目錄__init__.py里面都是空的。以下硬編碼代碼有效:import customer.jci.gt as ggt = g.get_gt()如何克服?
添加回答
舉報
0/150
提交
取消