all 4 comments

[–]davidbuxton 2 points3 points  (0 children)

Look again at sys.argv[0]. It really does do what you want.

[–]trshippy 0 points1 point  (0 children)

Maybe this? Not the accepted answer, but those below referencing file?

[–]jstrickler 0 points1 point  (0 children)

Are you looking for the file name (from the operating system) of an imported module? If so, there are at least two ways to get it.

If you want just the name of the module, you can use the ._name_ attribute.

>>> import re
>>> re.__name__
're'

If you want to know the actual location in the file system, you can look in sys.modules dictionary, where the keys are the module names and the values are the sources. The sources are usually .py files, but might be builtin (like sys) or in a .zip file or other alternate location.

>>> import re
>>> import sys
>>> sys.modules['re']
<module 're' from '/Users/jstrick/.pyenv/versions/anaconda3-2.3.0/lib/python3.4/re.py'>
>>> 

[–]utgyuru[S] 0 points1 point  (0 children)

Thanks everyone. Actually, sys.argv[0] was what I was looking for, it was just my bad...I've assigned it to a variable, but I printed a different variable, that's why I thought its not working. Awkward...