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
curdir = os.path.dirname(__file__)
sys.path.append(curdir + '/..')
Traceback (most recent call last):
File "C:\MyFile.py", line 3, in `<module>`
curdir = os.path.dirname(`__file__`)
NameError: name '`__file__`' is not defined
__file__ won't be set if you're writing this in the interpretor.
>>> 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
__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.