How to retrieve the filename of a module

Jim O'D jbo at cannedham.ee.ed.ac.uk
Thu Oct 20 12:37:13 EDT 2005


mku wrote:
> Hi,
> 
> there´s a function inside a module. How can these function retrieve
> the path+name   of his module ? (The path is most important).
> That should also work if the module is part of a package.
> 
> Thanks in advance
> 
> Martin
> 

Try the following in the function:

import traceback
f = traceback.extract_stack(limit=2)

If you output f to the interpreter, you'll see the filename but I don't 
know what position in the output list it is guaranteed to be.

Jim



More information about the Python-list mailing list