0

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?

3
  • What version of Python? Mar 2, 2018 at 15:10
  • 1
    Interesting question. I reproduced with command python -m idlelib.idle -r f:/python/a/tem2.py, where python runs 3.8.0 repository build, and tem2.py contains print(__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 by python 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. Mar 2, 2018 at 16:11
  • I'm using 3.6.4.
    – uwain12345
    Mar 5, 2018 at 8:25

1 Answer 1

2

In interactive mode, the input 'file' is the stream of statements entered by the user, so __file__ is left unset. If there is a PYTHONSTARTUP file, __file__ is set to that filename while the file is run, then unset before the first >>> prompt. Currently, IDLE does not do this when running user code in a separate process, which is now the normal mode.

I opened https://bugs.python.org/issue32984 to fix this. I will try to remember to report here when it is.

When IDLE is started with -n, to use the old deprecated mode in which user code is executed in the IDLE GUI process, startup files and interactive input see __file__ set to an idlelib file. Both are wrong but will not be fixed.

EDIT: PR-5981, which works for me on Windows, modifies pyshell.execfile to begin as follows:

def execfile(self, filename, source=None):  # currently line 633
    "Execute an existing file"
    if source is None:
        with tokenize.open(filename) as fp:
            source = fp.read()
            if use_subprocess:
                source = (f"__file__ = r'''{os.path.abspath(filename)}'''\n"
                          + source + "\ndel __file__")

The last 3 lines are new. uwain12345, adding them should solve your problem. If not, I would like to know.

EDIT 2: tweek replacement code to allow for ' in startup file name.

Note: f strings are new in 3.6. For older Pythons, replace the source line with

                filename = os.path.abspath(filename)
                source = ("__file__ = r'''{}'''\n".format(filename)
2
  • Thanks. I must admit I'm a bit reluctant to muck about in my Python installation.
    – uwain12345
    Mar 5, 2018 at 8:22
  • 1
    Caution is good. Upgrading to 3.6.5-32 when it is released will copy a new version of pyshell.py, with this fix, into your 3.6 directory. If you do not want to wait, adding 3 lines is about as safe as anything. Copy pyshell.py to pyshell.bak, edit pyshell.py by inserting 3 lines, save, and test. If necessary, rename pyshell.bak to pyshell.py. If you, or a Windows or disk glitch messes up python36-32/, run repair. I always keep the most recent installation file in a downloads directory in case this is needed. Mar 5, 2018 at 15:58

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.