Determine path of current module?

Gordon McMillan gmcm at hypernet.com
Thu Jul 20 19:34:53 EDT 2000


Carsten Gaebler wrote:

>"Jürgen Hermann" wrote:
>
>> ### mypath.py
>> import sys
>> print sys.modules[__name__].__file__
>> 
>> [d:\tmp]python -c "import mypath"
>> mypath.py
>
>Well, that's just the filename. What I'm looking for is the complete path,
>"d:\tmp\mypath.py" in this case.

First, you don't need "sys.modules[__name__].__file__", just "__file__". 

Second, it only lacks a path when it was found in '.', so 
os.path.abspath(__file__) will do fine for anything except the top level 
script (which doesn't have a __file__ attribute).

-Gordon



More information about the Python-list mailing list