[Python-Dev] sys.path[0]

Ka-Ping Yee ping@zesty.ca
Wed, 8 Jan 2003 03:01:49 -0600 (CST)


On 8 Jan 2003, Thomas Heller wrote:
> Actually I don't care whether sys.path[0] contains an absolute or relative
> pathname, but modules imported via a relative path entry get a mod.__file__
> attribute which is also a relative pathname.
>
> Changing the working directory then leads to strange results because
> mod.__file__ is no longer a valid pathname: think of reload(), tracebacks,
> and maybe more.

Exactly for this reason, changing the working directory confuses
inspect and pydoc and presumably anything else that tries to find
source code.  There's no way to work around this because the true
path information is simply not available, unless we fix the
__file__ attribute.

I'd be in favour of setting all __file__ attributes to absolute paths.

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

The current working directory is a piece of hidden global state.
In general, hidden global state is bad, and this particular piece
of state is especially important because it affects what Python
modules get loaded.  I'd prefer for the interpreter to just set
up sys.path correctly to begin with -- what's the point in doing
it ambiguously only to fix it up later anyway?


-- ?!ng