我不能用这个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()是目录内gt.py文件内的函数customer/jci。每个目录__init__.py里面都是空的。以下硬编码代码有效:import customer.jci.gt as ggt = g.get_gt()如何克服?
添加回答
举报
0/150
提交
取消