470,811 Members | 1,113 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 470,811 developers. It's quick & easy.

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

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

Jul 18 '05 #1
4 1684
Andreas Neudecker wrote:
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:ti****@stackless.com>
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/

Jul 18 '05 #2
Andreas Neudecker <a.*********@uni-bonn.de> writes:
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?
import os
os.__file__

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

Cheers,
mwh

--
Of the four project development variables - scope, cost, time and
quality - quality isn't really a free variable. The only possible
values are "excellent" and "insanely excellent", depending on
whether lives are at stake. -- Kent Beck, XP Explained
Jul 18 '05 #3
Danke, Christian! Hat mir sehr geholfen! :-)
Gruß
Andreas
Christian Tismer schrieb:
Andreas Neudecker wrote:
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

Jul 18 '05 #4
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 Hudson schrieb:
Andreas Neudecker <a.*********@uni-bonn.de> writes:

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?

import os
os.__file__


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

Cheers,
mwh

Jul 18 '05 #5

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

10 posts views Thread by Ben Finney | last post: by
5 posts views Thread by PaulS | last post: by
8 posts views Thread by inFocus | last post: by
reply views Thread by mihailmihai484 | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.