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

M.-A. Lemburg mal@lemburg.com
Thu, 22 Nov 2001 10:51:29 +0100


"James C. 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.  It is clear that
> sys.path should not change unless the user changes it.

I hope you mean user == programmer. Changing sys.path is 
perfectly legal and I wouldn't like to see that become illegal.
 
> 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.

True. 

This causes quite a bit of confusion sometimes, esp.
when people run scripts using relative paths and then find
that things don't work the way they expected.

I'm not sure if adding the absolute path would break anything,
though -- could be that some path fiddling code explicitly
looks for the '' in sys.path and then takes some action
based on the fact that the script was started from the CWD.
 
> If a command is entered using "-c", I don't think any insertion
> to sys.path should be made, as there is no indicated directory.
> Alternatively, the absolute path getcwd() should be inserted.

Same problem here: "-c" can be used as indicator... and probably
is by code looking for the absolute path of the script ;-)

-- 
Marc-Andre Lemburg
CEO eGenix.com Software GmbH
______________________________________________________________________
Consulting & Company:                           http://www.egenix.com/
Python Software:                        http://www.lemburg.com/python/