1

I had asked a question previously on relative paths in python in SO question: How can I access relative paths in Python 2.7 when imported by different modules

The provided answer worked great in all of my scripts and functions. However, when trying to debug the files in IDLE (Python 2.7) it generates run time errors.

Can anyone point me to documentation on using the __file__ notation? Also I would like to understand why IDLE generates errors while running the sample code but running the same file from the command line or double clicking it (for the windows users) does not.

Any help would be greatly appreciated!

Note that I am running Python 2.7 on Windows XP with virtualenv (unactivated during these tests).

Sample Code

import os
import sys
curdir = os.path.dirname(__file__) 
sys.path.append(curdir + '/..')

Error

Traceback (most recent call last):
File "C:\MyFile.py", line 3, in `<module>`
  curdir = os.path.dirname(`__file__`)
NameError: name '`__file__`' is not defined
Community
  • 1
  • 1
Adam Lewis
  • 7,017
  • 7
  • 44
  • 62
  • To properly compare how IDLE runs code (by running Python) with how Python runs code by itself, one must use the same run mode for each. The IDLE Shell compares with the interactive interpreter, started from a command line with 'python'. Running from an IDLE editor window compares with running the code from the command line with `python -i -m file. There are some differences that are intentional, some that have been fixed, perhaps some that should be fixed but have not yet been fixed, and some that are unavoidable. – Terry Jan Reedy Aug 28 '16 at 01:15

1 Answers1

3

__file__ won't be set if you're writing this in the interpretor.

So:

>>> import os, sys
>>> curdir = os.path.dirname(__file__)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined

Is expected.

__file__ is the name of the file that was called by the python interpretor - so if you ran this from a script it would work.

$ python curdir.py
$

(The script is exactly the same as what I put into the interpretor, hence no error or output)

From what I've observed using IDLE before, it acts as an interpretor - so it'll run the file in question. However, it wasn't started with that file, so the __file__ is never set.

TyrantWave
  • 4,583
  • 2
  • 22
  • 25
  • Thanks for the info. What you have said does make sense based on both of our observations. However, do you know where the documentation for the __file__ is located? Also any ideas on how to debug files with the __file__ notation? As they are quite important to project I am working on. Thanks again! – Adam Lewis Apr 04 '11 at 14:25
  • In response to the comment: "However, it wasn't started with that file, so the __file__ is never set." I actually right click the file and use the "Edit with IDLE" menu selection and run the file from IDLE to produce the errors. I am assuming that you mean that IDLE loads the file's content to run, but does not run the file itself? – Adam Lewis Apr 04 '11 at 14:26
  • `__file__` is set by [runpy](http://docs.python.org/library/runpy.html), which handles file loading. IDLE acts differently, it essentially performs an `exec` on the script, which doesn't set `__file__` - this is a bug in IDLE which still hasn't been fixed: [Run Module does not set __file__](http://bugs.python.org/issue8515) – TyrantWave Apr 04 '11 at 14:51
  • Any time :) Good luck with the debugging. – TyrantWave Apr 04 '11 at 15:00
  • @TyrantWave (and anyone else) The bug of __file__ not being set IDLE's Run => Run module was fixed in https://bugs.python.org/issue8515, first for 3.3 (2012-4-5) and then for 2.7 (2013-6-30). – Terry Jan Reedy Aug 28 '16 at 01:39