UNABLE TO GET IDLE TO RUN

Terry Reedy tjreedy at udel.edu
Tue Oct 27 07:18:42 EDT 2015


On 10/27/2015 4:15 AM, Peter Otten wrote:
> Terry Reedy wrote:

>> When one runs a
>> program with 'python somepath/file.py', python prepends somepath to
>> sys.path.
>
> If /foo/bar contains a string.py (for example) module and you run the
> following sequence
>
> $ cd /foo/bar
> $ python3 /usr/bin/idle3

I am curious what 'import sys; sys.path' prints in interactive mode, for 
both 'python3' and the above, of whichever *nix you are running.

> /foo/bar/string.py will shade string.py from the standard library.

Please reread what I wrote.  IDLE is imitating Python.  This shading the 
same behavior as if you just typed $ python3.  From 
https://docs.python.org/3/library/sys.html#sys.path

"sys.path

     A list of strings that specifies the search path for modules. 
Initialized from the environment variable PYTHONPATH, plus an 
installation-dependent default.

     As initialized upon program startup, the first item of this list, 
path[0], is the directory containing the script that was used to invoke 
the Python interpreter. If the script directory is not available (e.g. 
if the interpreter is invoked interactively or if the script is read 
from standard input), path[0] is the empty string, which directs Python 
to search modules in the current directory first. Notice that the script 
directory is inserted before the entries inserted as a result of PYTHONPATH.
"

Modules in the current directory shade stdlib modules of the same name. 
  This is a feature for people who want this behavior.  It is a problem 
for people who are caught by it.  The most common example here is 
beginners who write their own random.py, for instance, and then another 
file with 'import random', expecting to get the stdlib module.  This has 
nothing to do with IDLE.  If you do not like this behavior, criticize 
python, not IDLE.

"A program is free to modify this list for its own purposes."

IDLE's purpose is to imitate what python does, in both interactive and 
batch modes.  There are bugs relative to this (and I just opened 
https://bugs.python.org/issue25488), but the bug is not 'imitating 
python' but the failure to do so.  If you find a non-trivial divergence 
in how code is executed that I am not aware of, I would like to know.

> The relevant code in idle is
>
> https://hg.python.org/cpython/file/tip/Lib/idlelib/PyShell.py#l1522

> """
>      else:
>          dir = os.getcwd()
>          if dir not in sys.path:
>              sys.path.insert(0, dir)
> """

Nope, not in IDLE's normal mode, in which user code is executed in a 
separate process.  The above is for sys.process in the idle process.  I 
suspect it is a holdover from when Idle ran user code in its own process 
(which is still does when -n is given on the command line).

In the previous line, before 'else', the directory of all files given as 
args to be edited is added to sys.path.  I suspect that this is to 
enable completions in single process mode, which is deprecated.

> I think this is a misfeature.

Why do you think it a misfeature for IDLE to execute code the way Python 
does?

-- 
Terry Jan Reedy




More information about the Python-list mailing list