If an object is a module’s class or function I need to retrieve the absolute import path as a string. Example:
>>> from a.b.c import foo
>>> get_import_path(foo)
'a.b.c.foo'
I tried to look into inspect module but there’s nothing to do that.
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
What your are trying to do is inherently impossible.
foosimply does not know how you imported it — it might even have been imported in multiple different ways. Example on my Linux box:So
normpathandnormpath2are the same function object. It’s impossible to deduce the information in which way they were imported.That said, it sometimes might help to look at the
__module__attribute of your function:The
__module__attribute is not always defined, and if it is defined, it does not always contain the information you are looking for.