[Idle-dev] IDLEfork 0.9a2 - Duplicate items in sys.path

Kurt B. Kaiser kbk@shore.net
Sun, 02 Mar 2003 18:30:20 -0500


"David Harris" <dpharris76@msn.com> writes:

> When main() expands the current directory element (" ") of sys.path,
> a duplicate item will be added to sys.path if the current directory
> is a member of sys.path. For example, if my current directory is
> "C:\\Python22\\Scripts", I end up with two instances for this
> directory. That is: ['C:\\Python22\\Scripts',
> 'C:\\Python22\\Scripts', 'C:\\PYTHON22\\DLLs', 'C:\\PYTHON22\\lib',
> 'C:\\PYTHON22\\lib\\lib-tk', 'C:\\PYTHON22',
> 'C:\\PYTHON22\\lib\\site-packages']
>
> The duplicate(s) can be eliminated by changing:
>
>     # process sys.argv and sys.path:
>     for i in range(len(sys.path)):
>         sys.path[i] = os.path.abspath(sys.path[i])
>
> to:
>
>     # process sys.argv and sys.path:
>     temppath = []
>     for i in range(len(sys.path)):
>         pathitem = os.path.abspath(sys.path[i])
>         if pathitem not in temppath:
>             temppath.append(pathitem)
>     sys.path = temppath
>
> but is this really desirable (and does it even matter)? The new code
> will also drop duplicates which were present in PYTHONPATH. Is that
> good or bad?

The situation is a little complicated; it's a Python issue, seems to me. 

Windows
=======
When Python is started with a script:
sys.path[0]  is the directory of the script being run
        [1]         current working directory

unless we're just starting Python interactively w/o a script:
sys.path[0] is '', which expands to a duplicate.
        [1] is the current working directory    
So the path is already duplicated by the time it get to IDLE.

There is code a little further down in PyShell from the place you
noted which avoids further duplication.

You can play with this by running a script in a subdirectory of 
Scripts and doing things like

        ..\..\python ..\printpath.py

where printpath.py prints out the sys.path
etc.

Linux
=====
When Python is started with a script or from an executable file 
(i.e. first line is #!/usr/bin/env python)
sys.path[0] is the location of the executable file.

When IDLEfork is installed, sys.path[0] becomes /usr/bin.  This
kind of behaviour may be useful for introspection.

When Python is started interactively,
sys.path[0] is ''

IDLEfork adds the cwd if it's not already in sys.path.

There's really no great harm in the duplication.  My vote on
pruning the path is +0.  Maybe GvR has an opinion.

--
KBK