How to retrieve the filename of a module

M

mku

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
 
L

Lawrence Oluyede

Il 2005-10-20 said:
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.


rhymes@voodoo:~ $ cat > test.py
print __file__
import os
print os.path.abspath(__file__)

rhymes@voodoo:~ $ python test.py
test.py
/home/rhymes/test.py

Bye
 
L

Lawrence Oluyede

Il 2005-10-20 said:
rhymes@voodoo:~ $ cat > test.py
print __file__
import os
print os.path.abspath(__file__)

rhymes@voodoo:~ $ python test.py
test.py
/home/rhymes/test.py

Also __name__ for the name of the module
 
J

Jim O'D

mku said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,755
Messages
2,569,535
Members
45,007
Latest member
obedient dusk

Latest Threads

Top