How to get the full namespace of a module after its imported in python -
from foo.bar import bar print(get_namespace(bar))
i want above code snippet print "foo.bar.bar"
how in python3 ?
every imported module listed in sys.modules
. iterate through sys.modules
find path associated module:
import sys def get_module_name(mod): path, module in sys.modules.items(): if mod == module: return path raise valueerror('module not found') foo.bar import bar print(get_module_name(bar)) # foo.bar.bar
since word namespace
reserved a different concept, i've renamed function get_module_name
.
Comments
Post a Comment