I've written a little batch file (Windows 8.1) to start my script directly in IDLE:
START "" pythonw.exe "C:\Program Files (x86)\Python36-32\Lib\idlelib\idle.py" -r my_script.py
The script contains the line
my_dir = os.path.split(__file__)[0]
which produces a name error
Exception in Tkinter callback
Traceback (most recent call last):
File "C:\Program Files (x86)\Python36-32\lib\tkinter\__init__.py", line 1699, in __call__
return self.func(*args)
File "my_script.py", line 245, in out_file_dia
my_dir = os.path.split(__file__)[0]
NameError: name '__file__' is not defined
If I open IDLE first and then start the script, it works fine. Why is
__file__
not defined in this situation?
python -m idlelib.idle -r f:/python/a/tem2.py
, wherepython
runs 3.8.0 repository build, andtem2.py
containsprint(__name__, __file__)
. The latter ran fine from editor. If I add-n
before-r
, so that tem2.py executes in the IDLE process, file is the IDLE process file.../idlelib/idle.py
.set PYTHONSTARTUP=f:/python/a/tem2.py
followed bypython
prints__main__ f:/python/a/tem2.py
.>>> __file__
results in the exception. Python sets file for startup file, unsets in interactive input. IDLE does not. Bug.