Interview Questions

__import__('x.y.z') returns < module 'x'>; how do I get z?

Python Questions and Answers


(Continued from previous question...)

__import__('x.y.z') returns < module 'x'>; how do I get z?

Try:
__import__('x.y.z').y.z

For more realistic situations, you may have to do something like
m = __import__(s)
for i in s.split(".")[1:]:
m = getattr(m, i)

(Continued on next question...)

Other Interview Questions