[Python-Dev] sys.path[0]

Guido van Rossum guido@python.org
Tue, 07 Jan 2003 20:15:19 -0500


> [Kevin Altis brought this to my attention, so I'm cc-ing him]
> 
> The python docs state on sys.path:
> 
>   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.
> 
> This is at least misleading.
> 
> It appears that for certain ways Python is started, the first item
> on sys.path is a relative path name, or even empty, even if a script
> was specified, and the path would have been available.

What's wrong with a relative pathname?  If you invoke the script using
a relative pathname, why shouldn't that be what you get?  The docs
don't say that it's the absolute pathname, so I don't think you can
claim that the docs are misleading here.

> This leads to problems if the script changes the working directory.
> Not always because the programmer explicitely called os.chdir(),
> also 'behind the scenes' when a GUI is used.

Why would a GUI change the current directory?  That seems pretty
broken.

> Shouldn't Python convert sys.path to absolute path names, to avoid
> these problems?

site.py converts sys.path entries to absolute pathnames, *except* for
the path entry for to the script directory, because that is added to
sys.path *after* site.py is run.

I'm disinclined to do anything about this, except perhaps warn that
the script directory may be given as a relative path.

--Guido van Rossum (home page: http://www.python.org/~guido/)