Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Retrieving a modules dotted path name

Tags:

python

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?

like image 643
Dan Oblinger Avatar asked Jul 10 '26 03:07

Dan Oblinger


1 Answers

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.

like image 83
Heath Raftery Avatar answered Jul 11 '26 19:07

Heath Raftery



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!