__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)

CATEGORY: programming

 

Comment:

Marius Gedminas suggests that m = __import__(s, {}, {}, []), and notes that Zope3 uses this a lot. Although this contradicts the standard library documentation (http://www.python.org/doc/current/lib/built-in-funcs.html), which claims that __import__('x.y.z', {}, {}, fromlist) would only return z if fromlist is non-empty. Bug in the documentation?

Posted by amk

A Django site. this page was rendered by a django application in 0.02s 2008-11-21 13:29:22.596063. hosted by webfaction.