This issue tracker has been migrated to GitHub, and is currently read-only.
For more information, see the GitHub FAQs in the Python's Developer Guide.

Title: IDLE: set and unset __file__ for startup files
Type: behavior Stage: resolved
Components: IDLE Versions: Python 3.8, Python 3.7, Python 3.6
Status: closed Resolution: fixed
Dependencies: Superseder:
Assigned To: terry.reedy Nosy List: cheryl.sabella, miss-islington, terry.reedy
Priority: normal Keywords: patch

Created on 2018-03-02 17:13 by terry.reedy, last changed 2022-04-11 14:58 by admin. This issue is now closed.

Pull Requests
URL Status Linked Edit
PR 5981 merged terry.reedy, 2018-03-05 06:05
PR 5986 merged miss-islington, 2018-03-05 16:02
PR 5987 closed miss-islington, 2018-03-05 16:03
PR 5993 merged miss-islington, 2018-03-05 18:48
Messages (8)
msg313140 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-03-02 17:13
'python' sets main.__file__ to ''. 'python' leaves __file__ unset.  If PYTHONSTARTUP is set to, 'python' executes in main with __file__ set to '', then unsets __file__ before the >>> prompt, as if somefile has not been executed.  Any explicit setting of __file__ in somefile is undone.
print(__name__, __file__)
__file__ = ''

> F:\dev\3x> set PYTHONSTARTUP=f:/python/a/
> F:\dev\3x> python
__main__ f:/python/a/
>>> __file__
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
NameError: name '__file__' is not defined

With IDLE, when 'python -m idlelib.idle' is run with '-s' or '-r f:/python/a/', NameError is raised for the print in This was reported this SO question. 

In both cases, the file is run with execfile(filename).

    def execfile(self, filename, source=None):
        "Execute an existing file"
        if source is None:
            with as fp:
                source =

My guess is that wrapping the source with f"__file__ = {filename}\n" and "del __file__\n" should work.
msg313151 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-03-02 17:52
When the IDLE GUI process is started, its main.__file__ is set to the idlelib .py file used to start the process.  If IDLE is started with -n, so that user code is run in the same process (the original mode), the -r or -s startup file and code entered at >>> sees this value*, rather than the user startup file name or a NameError.

* I am not sure why it does not see the value of __file__ in the pyshell module, which I would expect to be '.../idlelib/'.

The change proposed above would fix __file__ for -n also, but would disable any subsequently executed idle code that uses __file__ that gets unset.  So I think the wrapping should be conditioned on use_subprocess (True unless -n).
msg313152 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-03-02 18:00
Revised, more exact, URL:
msg313238 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-03-05 06:43
With the PR, IDLE behaves the same as Python on Windows.
   .../3x> python -m idlelib -r f:/python/a/
(see original post for content of
prints __file__ instead of raising NameError, while
  >>> __file__ continues to raise NameError

Cheryl, I really expect the PR to work on linux, but can you manually test it anyway?  If you think the blurb needs rst markup and feel like adding some, go ahead.
msg313248 - (view) Author: Cheryl Sabella (cheryl.sabella) * (Python committer) Date: 2018-03-05 13:59
The change works on Linux with the patch (and gave a NameError without the patch).  I left a note on the PR for a possible SyntaxError.

I didn't have authority to push the blurb markup change to your branch, so I copied it into a comment.  If you click edit on it, you should be able to cut and paste it as is into the blurb file.
msg313254 - (view) Author: Terry J. Reedy (terry.reedy) * (Python committer) Date: 2018-03-05 16:02
New changeset 22c82be5df70c3d51e3f89b54fe1d4fb84728c1e by Terry Jan Reedy in branch 'master':
bpo-32984: IDLE - set __file__ for startup files (GH-5981)
msg313267 - (view) Author: miss-islington (miss-islington) Date: 2018-03-05 16:49
New changeset fd340bf9e308130736c76257ff9a697edbeb082d by Miss Islington (bot) in branch '3.7':
bpo-32984: IDLE - set __file__ for startup files (GH-5981)
msg313280 - (view) Author: miss-islington (miss-islington) Date: 2018-03-05 19:23
New changeset 6935a511670797a3aaebdf96aad3dcff66baa76e by Miss Islington (bot) in branch '3.6':
bpo-32984: IDLE - set __file__ for startup files (GH-5981)
Date User Action Args
2022-04-11 14:58:58adminsetgithub: 77165
2018-03-05 19:46:08terry.reedysetstatus: open -> closed
resolution: fixed
stage: patch review -> resolved
2018-03-05 19:23:11miss-islingtonsetmessages: + msg313280
2018-03-05 18:48:15miss-islingtonsetpull_requests: + pull_request5760
2018-03-05 16:49:20miss-islingtonsetnosy: + miss-islington
messages: + msg313267
2018-03-05 16:03:55miss-islingtonsetpull_requests: + pull_request5754
2018-03-05 16:02:58miss-islingtonsetstage: needs patch -> patch review
pull_requests: + pull_request5753
2018-03-05 16:02:48terry.reedysetmessages: + msg313254
2018-03-05 13:59:54cheryl.sabellasetmessages: + msg313248
2018-03-05 06:43:40terry.reedysetnosy: + cheryl.sabella

messages: + msg313238
stage: patch review -> needs patch
2018-03-05 06:05:13terry.reedysetkeywords: + patch
stage: needs patch -> patch review
pull_requests: + pull_request5747
2018-03-02 18:00:19terry.reedysetmessages: + msg313152
2018-03-02 17:52:55terry.reedysetmessages: + msg313151
2018-03-02 17:13:39terry.reedycreate