Proper use of __file__

Discussion in 'Python' started by Darren Dale, Oct 5, 2004.

  1. Darren Dale

    Darren Dale Guest

    Would somebody explain how to use __file__? I mean, what information it
    carries, and more importantly, when it does and does not exist? A one line
    script:

    print __file__

    will yield the relative path if run from a shell, but will raise NameError
    when the script is run interactively from the interpreter. Is this the
    intended behavior?

    Thanks,
    Darren
     
    Darren Dale, Oct 5, 2004
    #1
    1. Advertising

  2. Darren Dale wrote:

    > Would somebody explain how to use __file__? I mean, what information it
    > carries, and more importantly, when it does and does not exist? A one line
    > script:
    >
    > print __file__
    >
    > will yield the relative path if run from a shell, but will raise NameError
    > when the script is run interactively from the interpreter. Is this the
    > intended behavior?


    __file__ is the name of the file in which the statement containing
    "__file__" appears. If your file foo.py reads:

    print __file__

    You can run "import foo" from the interpreter, and it will print "foo.py",
    just like if you ran "python foo.py". But if you run "print __file__" in
    the interpreter directly, NameError is raised because the call is not being
    made from any file, so __file__ is undefined.

    It is most certainly intended.

    Hope that helps,
    Jeffrey
     
    Jeffrey Froman, Oct 5, 2004
    #2
    1. Advertising

  3. Darren Dale

    Aahz Guest

    In article <>,
    Jeffrey Froman <> wrote:
    >
    >__file__ is the name of the file in which the statement containing
    >"__file__" appears. If your file foo.py reads:
    >
    >print __file__
    >
    >You can run "import foo" from the interpreter, and it will print "foo.py",
    >just like if you ran "python foo.py". But if you run "print __file__" in
    >the interpreter directly, NameError is raised because the call is not being
    >made from any file, so __file__ is undefined.


    One gotcha: in Python 2.2 and earlier, __file__ is only defined for
    imported modules; "python foo.py" will raise a NameError.
    --
    Aahz () <*> http://www.pythoncraft.com/

    WiFi is the SCSI of the 21st Century -- there are fundamental technical
    reasons for sacrificing a goat. (with no apologies to John Woods)
     
    Aahz, Oct 5, 2004
    #3
  4. Darren Dale

    David Bolen Guest

    (Aahz) writes:

    > In article <>,
    > Jeffrey Froman <> wrote:
    > >
    > >__file__ is the name of the file in which the statement containing
    > >"__file__" appears. If your file foo.py reads:
    > >
    > >print __file__
    > >
    > >You can run "import foo" from the interpreter, and it will print "foo.py",
    > >just like if you ran "python foo.py". But if you run "print __file__" in
    > >the interpreter directly, NameError is raised because the call is not being
    > >made from any file, so __file__ is undefined.

    >
    > One gotcha: in Python 2.2 and earlier, __file__ is only defined for
    > imported modules; "python foo.py" will raise a NameError.


    And even in later versions it can also be a problem for your main
    script if you've packaged up your script into a standalone setup
    (e.g., with installer/py2exe).

    -- David
     
    David Bolen, Oct 5, 2004
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Spry
    Replies:
    1
    Views:
    959
    Richard Heathfield
    Jul 26, 2003
  2. qazmlp

    Definition of __FILE__, __LINE__

    qazmlp, Aug 9, 2003, in forum: C Programming
    Replies:
    9
    Views:
    629
    Mark McIntyre
    Aug 19, 2003
  3. Replies:
    5
    Views:
    334
    Keith Thompson
    Mar 12, 2005
  4. klenwell

    __file__ vs __FILE__

    klenwell, Nov 3, 2007, in forum: Python
    Replies:
    11
    Views:
    936
    Matimus
    Nov 5, 2007
  5. H. Wade Minter
    Replies:
    8
    Views:
    309
    Robin
    Apr 25, 2004
Loading...

Share This Page