Dan Oblinger
Dan Oblinger

Reputation: 534

Retrieving a modules dotted path name

suppose I import a package

import foo.bar.baz

I can find this module's filepath at sys.modules['foo.bar.baz'].__file__. From this I could try to reconstruct the 'foo.bar.baz' package name. But this seems wrong.

Given a module, in variable, m, how do I get the dotted pathname that would be used to import it?

Upvotes: 3

Views: 1024

Answers (1)

Heath Raftery
Heath Raftery

Reputation: 4169

The __name__ attribute should do it. See the link for the subtle difference between it and __package__ and choose appropriately.

>>> import xml.dom.minidom as m
>>> m.__name__
'xml.dom.minidom'

See PEP 366 for further details.

Upvotes: 4

Related Questions