Find out the file name of a module from inside the module?

  • Thread starter Andreas Neudecker
  • Start date
A

Andreas Neudecker

Hi.

I know you can read the filename of a program as sys.argv[0]. But what
about modules? Is there a similar way to find out the file name of a
module (called by some other module or program) from inside this module?

Kind regards


Andreas
 
M

Michael Hudson

Andreas Neudecker said:
Hi.

I know you can read the filename of a program as sys.argv[0]. But what
about modules? Is there a similar way to find out the file name of a
module (called by some other module or program) from inside this
module?

Uh, your question makes my head spin a bit, but is __file__ what you
want?
'/usr/lib/python2.2/os.pyc'

Cheers,
mwh
 
C

Christian Tismer

Andreas said:
Hi.

I know you can read the filename of a program as sys.argv[0]. But what
about modules? Is there a similar way to find out the file name of a
module (called by some other module or program) from inside this module?

If your modules knows that it is a module, it can inspect its
__file__ attribute.
You can find out whether you have been imported by testing
whether there is a __file__ attribute, otherwise you are
probably executed, and there is no name available.

try:
fname = __file__
except NameError:
fname = "?"

In cases where you absolutely need a file name, and if you know
that you are also used as a module, you can of course import
yourself, in order to get the file name. This implies the need
to know your module name, which doesn't make things so much better:

# assuming this is MyModule
import MyModule
fname = MyModule.__file__
del MyModule

Or even shorter:

from MyModule import __file__ as fname

ciao - chris
--
Christian Tismer :^) <mailto:[email protected]>
Mission Impossible 5oftware : Have a break! Take a ride on Python's
Johannes-Niemeyer-Weg 9a : *Starship* http://starship.python.net/
14109 Berlin : PGP key -> http://wwwkeys.pgp.net/
work +49 30 89 09 53 34 home +49 30 802 86 56 mobile +49 173 24 18 776
PGP 0x57F3BF04 9064 F4E1 D754 C2FF 1619 305B C09C 5A3B 57F3 BF04
whom do you want to sponsor today? http://www.stackless.com/
 
A

Andreas Neudecker

Danke, Christian! Hat mir sehr geholfen! :)


Gruß


Andreas


Christian said:
Andreas said:
Hi.

I know you can read the filename of a program as sys.argv[0]. But what
about modules? Is there a similar way to find out the file name of a
module (called by some other module or program) from inside this module?


If your modules knows that it is a module, it can inspect its
__file__ attribute.
You can find out whether you have been imported by testing
whether there is a __file__ attribute, otherwise you are
probably executed, and there is no name available.

try:
fname = __file__
except NameError:
fname = "?"

In cases where you absolutely need a file name, and if you know
that you are also used as a module, you can of course import
yourself, in order to get the file name. This implies the need
to know your module name, which doesn't make things so much better:

# assuming this is MyModule
import MyModule
fname = MyModule.__file__
del MyModule

Or even shorter:

from MyModule import __file__ as fname

ciao - chris
 
A

Andreas Neudecker

Hi Michael,

sorry for making your head spin ;-)
I think, Christian's answer is exactly what I was looking for: Just cut
out the following code snippets and put them in files called program.py
and module.py, respectively. Run program.py and you will see ... ;-)


Thanks to both of you, Michael, Christian.


Kind regards


Andreas


program.py:
8<------------------------------------------------------------------------

#!/usr/bin/env python

import sys
import os

# Check if running as program
if __name__ == '__main__':
print "I was run as a PROGRAM and my path is \n\n\t'%s'\n" %
os.path.abspath (sys.argv[0])
print "Now, I will call the module 'module' ..."
import module
print "Let's see, what 'module' says about itself, calling
'module.whoami()':\n\n"
module.whoami()

else:
print "Hey, I was not run as a program. What's on?"

# EOF
-------------------------------------------------------------------------#


module.py
8<------------------------------------------------------------------------

# Module 'module.py'
----------------------------------------------------------#

try:
THIS_FILE = __file__
except NameError:
print "I guess I was not called as a module. That's strange."

def whoami ():
print "I was called as a MODULE and my path is \n\n\t'%s'\n" % THIS_FILE

# EOF
-------------------------------------------------------------------------#



Michael said:
Hi.

I know you can read the filename of a program as sys.argv[0]. But what
about modules? Is there a similar way to find out the file name of a
module (called by some other module or program) from inside this
module?


Uh, your question makes my head spin a bit, but is __file__ what you
want?


'/usr/lib/python2.2/os.pyc'

Cheers,
mwh
 

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

No members online now.

Forum statistics

Threads
473,754
Messages
2,569,526
Members
44,997
Latest member
mileyka

Latest Threads

Top