argv[0] manipulation

Bernhard Herzog bh at intevation.de
Sun Dec 15 08:34:16 EST 2002


Erik Max Francis <max at alcyone.com> writes:

> Bernhard Herzog wrote:
> 
> > "James Kew" <james.kew at btinternet.com> writes:
> >
> > > Don't rely on this being portable: the sys module documentation
> > > (http://www.python.org/doc/current/lib/module-sys.html) makes no
> > > such
> > > guarantees about sys.argv[0].
> > 
> > Then the documentation is wrong, or at least misleading :)
> 
> On the contrary!  The documentation is specifying the "contract" between
> you and the interpreter.  The documentation is saying, "Don't rely on
> this being useful for anything."

Not exactly. It says for the case where Python has been started with a
script filename as argument:

    argv[0] is the script name (it is operating system dependent whether
    this is a full pathname or not).

It's not clear to me that this means that it's not useful for anything.
It only says that it's OS dependend whether it's a "full pathname".
What's a "full pathname"? An "absolute pathname"?

The operating system *has* to pass a usable absoulte or relative
filename to the Python interpreter. Whether Python puts that filename or
a modified version of it into sys.argv[0] is up to Python and
independend of the OS Python is running on, so python could make some
guarantees about it.

Furthermore, Python itself uses the original filename it was given to
put the directory containing the script into sys.path and the
documentation doesn't qualify that with "operating system dependend":

   The first item of this list, path[0], is the directory containing the
   script that was used to invoke the Python interpreter.

> The documentation is sending you a very important message:  It's saying
> not to rely on sys.argv[0] being of much use for anything beyond a vague
> hint at what script is running.

I suppose it can be read that way, but IMO that wasn't really the
intention of the documentation authors.

   Bernhard

-- 
Intevation GmbH                                 http://intevation.de/
Sketch                                 http://sketch.sourceforge.net/
MapIt!                                               http://mapit.de/



More information about the Python-list mailing list