By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
434,573 Members | 938 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 434,573 IT Pros & Developers. It's quick & easy.

__file__

P: n/a
Hi,

I'm having trouble understanding what the definition of __file__ is.
With this program:

------
#data.py:

def show():
print __file__

if __name__ == "__main__":
show()
-------

if I run data.py with the prompt pointing to the directory that
contains data.py, then __file__ produces a filename:

data.py

If I run data.py with the prompt pointing to a different directory,
then file produces what I entered on the command line, e.g.:

../2testing/dir1/data.py
If I import the data module into another python program, e.g.:

-------
#test1.py:

from data import show
show()
------

and test1.py is in the same directory as data.py, __file__ produces an
absolute path to the data module:

/Users/me/2testing/dir1/data.pyc

If test1.py is in a different directory than data.py, __file__
produces the path used in sys.path.append(), e.g.:

----
import sys
sys.path.append("./2testing/dir1")

import data
data.show()
---output:------
../2testing/dir1/data.pyc

====or======

import sys
sys.path.append("/Users/me/2testing/dir1")

import data
data.show()
---output:-------
/Users/me/2testing/dir1/data.pyc
And some modules have __file__ in their __dict__ and some don't:

-------
import sys, pprint, data

pprint.pprint(data.__dict__)
print
print "*******"
print
pprint.pprint(sys.__dict__)

Apr 11 '07 #1
Share this Question
Share on Google+
4 Replies


P: n/a
En Tue, 10 Apr 2007 21:20:51 -0300, 7stud <bb**********@yahoo.com>
escribió:
I'm having trouble understanding what the definition of __file__ is.
With this program:

------
#data.py:

def show():
print __file__

if __name__ == "__main__":
show()
-------

if I run data.py with the prompt pointing to the directory that
contains data.py, then __file__ produces a filename:

data.py
[cut: other examples showing different paths to data.py]

__file__ corresponds to the filename used to locate and load the module,
whatever it is. When the module is found on the current directory
(corresponding to '' in sys.path), you get just the filename; if sys.path
contains a relative path, that's what you get; the same for any absolute
path.
Whatever path worked to find and load the module, that's stored as
__file__.

If you plan to use it, it's a good idea to make it early into an absolute
path (using os.path.abspath(__file__)) just in case the current directory
changes.
And some modules have __file__ in their __dict__ and some don't:
pprint.pprint(sys.__dict__)
sys is a builtin module: no sys.py/sys.pyc exists, so __file__ is
meaningless.

--
Gabriel Genellina

Apr 11 '07 #2

P: n/a
Hi,

Thanks for the response.

On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
>
__file__ corresponds to the filename used to locate and load the module,
whatever it is. When the module is found on the current directory
(corresponding to '' in sys.path), you get just the filename; if sys.path
contains a relative path, that's what you get; the same for any absolute
path.
Whatever path worked to find and load the module, that's stored as
__file__.

If you plan to use it, it's a good idea to make it early into an absolute
path (using os.path.abspath(__file__)) just in case the current directory
changes.
That last part doesn't seem to fit with your description above. What
does the current working directory have to do with the path that was
used to load a module? I would think the path that was used to load a
module is constant.

Apr 11 '07 #3

P: n/a
On Apr 11, 8:03 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
Hi,

Thanks for the response.

On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
__file__ corresponds to the filename used to locate and load the module,
whatever it is. When the module is found on the current directory
(corresponding to '' in sys.path), you get just the filename; if sys.path
contains a relative path, that's what you get; the same for any absolute
path.
Whatever path worked to find and load the module, that's stored as
__file__.
If you plan to use it, it's a good idea to make it early into an absolute
path (using os.path.abspath(__file__)) just in case the current directory
changes.

That last part doesn't seem to fit with your description above. What
does the current working directory have to do with the path that was
used to load a module? I would think the path that was used to load a
module is constant.
You are correct, but that is not what GG was talking about. Here is an
example of what he meant:

While your cwd is /bar, you load module foo from a *relative*( path,
e.g. ./foo.py. If you do the os.path.abspath("./foo.py") immediately
as recommended, you get the correct answer: /bar/foo.py. However if
you change your cwd to /zot, then do the os.path.abspath("./foo.py"),
you get /zot/foo.py which is a nonsense.

HTH,
John

Apr 11 '07 #4

P: n/a
On Apr 11, 6:55 am, "John Machin" <sjmac...@lexicon.netwrote:
On Apr 11, 8:03 pm, "7stud" <bbxx789_0...@yahoo.comwrote:
Hi,
Thanks for the response.
On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-...@yahoo.com.ar>
wrote:
__file__ corresponds to the filename used to locate and load the module,
whatever it is. When the module is found on the current directory
(corresponding to '' in sys.path), you get just the filename; if sys.path
contains a relative path, that's what you get; the same for any absolute
path.
Whatever path worked to find and load the module, that's stored as
__file__.
If you plan to use it, it's a good idea to make it early into an absolute
path (using os.path.abspath(__file__)) just in case the current directory
changes.
That last part doesn't seem to fit with your description above. What
does the current working directory have to do with the path that was
used to load a module? I would think the path that was used to load a
module is constant.

You are correct, but that is not what GG was talking about. Here is an
example of what he meant:

While your cwd is /bar, you load module foo from a *relative*( path,
e.g. ./foo.py. If you do the os.path.abspath("./foo.py") immediately
as recommended, you get the correct answer: /bar/foo.py. However if
you change your cwd to /zot, then do the os.path.abspath("./foo.py"),
you get /zot/foo.py which is a nonsense.

HTH,
John
Thanks for all the help.

Apr 12 '07 #5

This discussion thread is closed

Replies have been disabled for this discussion.