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

Search Discussions

  • Gabriel Genellina at Apr 11, 2007 at 6:49 am
    En Tue, 10 Apr 2007 21:20:51 -0300, 7stud <bbxx789_05ss at 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
  • 7stud at Apr 11, 2007 at 10:03 am
    Hi,

    Thanks for the response.
    On Apr 11, 12:49 am, "Gabriel Genellina" 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.
  • John Machin at Apr 11, 2007 at 12:55 pm

    On Apr 11, 8:03 pm, "7stud" wrote:
    Hi,

    Thanks for the response.

    On Apr 11, 12:49 am, "Gabriel Genellina" 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
  • 7stud at Apr 12, 2007 at 6:34 am

    On Apr 11, 6:55 am, "John Machin" wrote:
    On Apr 11, 8:03 pm, "7stud" wrote:


    Hi,
    Thanks for the response.
    On Apr 11, 12:49 am, "Gabriel Genellina" <gagsl-... at 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.

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedApr 11, '07 at 12:20a
activeApr 12, '07 at 6:34a
posts5
users3
websitepython.org

People

Translate

site design / logo © 2022 Grokbase