Issue1548501

classification
Title: __file__ not available when running file as a script
Type: Severity: normal
Components: None Versions:
Milestone:
process
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: cgroves Nosy List: cgroves, nobody, pekka.klarck
Priority: normal Keywords:

Created on 2006-08-29.11:41:57 by pekka.klarck, last changed 2006-09-07.03:47:22 by cgroves.

Messages
msg1217 (view) Author: Pekka Klärck (pekka.klarck) Date: 2006-08-29.11:41:57
It seems that __file__ is not available when running
file as a script even though it is there when you
import it as a module.

There's also small inconsistency between Jython and
CPython because in Jython the file is returned in
absolute format but in CPython it is relative to the
directory where Python is run. This may in some cases
cause problem when you run code developed for CPython
in Jython -- when developing code for both you realize
that you have to use os.path.abspath or
os.path.basename depending what you want.


Example:

$ echo 'print __file__' > test.py

$ jython test.py 
Traceback (innermost last):
  File "test.py", line 1, in ?
NameError: __file__

$ jython
Jython 2.2a1 on java1.5.0_04 (JIT: null)
Type "copyright", "credits" or "license" for more
information.
>>> import test
c:\path\test.py
>>> 

$ python test.py 
test.py

$ python
Python 2.4.3 (#1, May 18 2006, 07:40:45) 
[GCC 3.3.3 (cygwin special)] on cygwin
Type "help", "copyright", "credits" or "license" for
more information.
>>> import test
test.py
>>> 

msg1218 (view) Author: Nobody/Anonymous (nobody) Date: 2006-08-29.19:59:27
Logged In: NO 

Just noticed that there's already another __file__ related
bug #1284344 that even has this issue mentioned in its
comments. The real issue reported there makes using __file__
anywhere in production code quite dangerous.
msg1219 (view) Author: Charlie Groves (cgroves) Date: 2006-09-07.03:47:22
Logged In: YES 
user_id=1174327

Fixed in r2925 tested by test393
History
Date User Action Args
2006-08-29 11:41:57pekka.klarckcreate