No, IDLE compiles the module (with the 'compile' built-in using the 'exec' option) and then does an 'exec' on the code (in PyShell.py).  It has several lines of code that it runs before this exec to prepare the environment that the code is run in.  It appears to be an oversight that the __file__ variable is not being set as a part of this preparation code to match the behavior of the python CLI.  The patch that I included only changes one line of this preparation code to also set the __file__ variable and that fixes the problem.  If you examine the IDLE code in the immediate vicinity of my patch you will see this.

I have several use cases where I'm relying on the __file__ variable in my module so that it can find other non .py files that it needs in the same directory that it's in.  This works under all combinations of uses from the CLI, but fails in IDLE using Run Module.

The language reference manual states under "Module":

Predefined (writable) attributes: __name__ is the module’s name; __doc__ is the module’s documentation string, or None if unavailable; __file__ is the pathname of the file from which the module was loaded, if it was loaded from a file. The __file__ attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.

The python CLI honors this definition in all cases, but IDLE/Run Module does not.

On Fri, Jun 11, 2010 at 11:11 AM, Tal Einat <report@bugs.python.org> wrote:

Tal Einat <taleinat@users.sourceforge.net> added the comment:

I believe IDLE runs modules via execfile(), so I would expect the behavior to be similar, and execfile() does not set __file__. Doing "Run Module" is also IMO equivalent to doing execfile(), so this behavior retains consistency.

However, I would expect __file__ to be set when running IDLE -r <script>, but I get "name '__file__' is not defined" (with Python 2.6.2). This is inconsistent and should be fixed.

----------
nosy: +taleinat

_______________________________________
Python tracker <report@bugs.python.org>
<http://bugs.python.org/issue8515>
_______________________________________