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?
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With