[Python-Dev] Change to sys.path[0] for Python 2.3

Gordon McMillan gmcm@hypernet.com
Wed, 21 Nov 2001 16:48:01 -0500


Jim "Fortran" Ahlstrom wrote:

> I have been working on import.c and thinking about
> imports generally.  Currently, the directory of the
> Python script is inserted into sys.path[0].  For example,
> "python /A/B/myscript.py" creates sys.path[0] = "/A/B", and
> "python myscript.py" creates sys.path[0] = "".  But there
> are three problems.
> 
> This insertion occurs after a number of imports have already
> occurred.  Specifically, it occurs after the import of site, os,
> and sitecustomize.  This is confusing.

Why? site happens before Python even thinks about 
sys.argv[0]. By it's very name it's about "how this installation 
should behave", not "how a script in this directory behaves".

>  It is clear that sys.path
> should not change unless the user changes it.

I am +1 (for very large values of 1) on clarifying the rules of 
import, but while hidden manipulations of sys.path qualify as a 
sneaky trick, I don't think they can be outlawed.

> If no path component is given, the zero length string is
> inserted.  But if the current working directory later
> changes, this is no longer valid.  If we want the directory
> of the script to be sys.path[0], then an absolute path should be
> inserted.

Some people os.chdir() just for this effect. I don't think I mind if 
they experience some pain <wink>.



- Gordon