I can't use this
import foo
method_to_call = getattr(foo, 'bar')
result = method_to_call()
because module name is hardcoded and I can't use this
module = __import__('foo')
func = getattr(module, 'bar')
func()
because module is nested.
I tried this
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()
but failed with errors, specified in comment along with each variant.
get_gt() is a function inside gt.py file inside customer/jci directory. Each directory has empty __init__.py inside.
The following hardcoded code works:
import customer.jci.gt as g
gt = g.get_gt()
How to overcome?
1 Answer 1
What you want is importlib.import_module.
Note that __import__ does handle dotted names but it returns the parent package, not the last child.
Proof:
>>> http = __import__('http.client')
>>> http.client # the client submodule was imported
<module 'http.client' from '/usr/lib/python3.6/http/client.py'>
>>> # without specifying the .client in the name the submodule is not imported
>>> __import__('http').client
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
AttributeError: module 'http' has no attribute 'client'
import_module instead returns the child module, which is what most people expect:
>>> importlib.import_module('http.client')
<module 'http.client' from '/usr/lib/python3.6/http/client.py'>
3 Comments
package.module.class.method and get a reference to the method. The OP already knows how to retriever an attribute using getattr, the only issue they had is the one I explained in the answer.