Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > Python > Proper use of __file__

Reply
Thread Tools

Proper use of __file__

 
 
Darren Dale
Guest
Posts: n/a
 
      10-05-2004
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
 
Reply With Quote
 
 
 
 
Jeffrey Froman
Guest
Posts: n/a
 
      10-05-2004
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
 
Reply With Quote
 
 
 
 
Aahz
Guest
Posts: n/a
 
      10-05-2004
In article <(E-Mail Removed)>,
Jeffrey Froman <(E-Mail Removed)> 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 ((E-Mail Removed)) <*> 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)
 
Reply With Quote
 
David Bolen
Guest
Posts: n/a
 
      10-05-2004
http://www.velocityreviews.com/forums/(E-Mail Removed) (Aahz) writes:

> In article <(E-Mail Removed)>,
> Jeffrey Froman <(E-Mail Removed)> 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
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
__file__ vs __FILE__ klenwell Python 11 11-05-2007 06:50 PM
can I use __FILE__ ,__LINE__ etc. in a code witout DEBUG defined? baumann.Pan@gmail.com C Programming 5 03-12-2005 12:29 AM
Proper way to use an imported constant under 'use strict'? H. Wade Minter Perl Misc 8 04-25-2004 12:58 AM
Definition of __FILE__, __LINE__ qazmlp C Programming 9 08-18-2003 11:13 PM
Macro for memory logging using __FILE__ and __LINE__ Spry C Programming 1 07-26-2003 01:13 PM



Advertisments