__file__

7

7stud

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__)
 
G

Gabriel Genellina

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.
 
7

7stud

Hi,

Thanks for the response.

__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.
 
J

John Machin

Hi,

Thanks for the response.




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
 
7

7stud

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.
 

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,536
Members
45,020
Latest member
GenesisGai

Latest Threads

Top