How to get path of a .py script

Bernhard Herzog bh at intevation.de
Wed Dec 11 15:11:59 EST 2002


"David Necas (Yeti)" <yeti at physics.muni.cz> writes:

> On Wed, Dec 11, 2002 at 08:05:12PM +0100, Bernhard Herzog wrote:
> > 
> > Indeed. If the filename given to the Python interpreter is a symlink to
> > the actual script, Python puts the directory containing the actual
> > script into sys.path as the first item. So, sys.path[0] is usually
> > enough.

I should perhaps add, that this also works if the filename is not a
symlink.

> But if the link contains ../ components, they also appear in
> sys.path[0], which I don't call fully resolved.

Well, the original poster just wanted to get the correct directory. That
doesn't require resolving all symlinks. In fact he didn't even say
whether it was a symlink in the first place (a link from a desktop is
not necessarily a symlink).

> And what's even funnier, when it's not just a single link,
> but a link chain, you get just intermediate path after first
> link resolved in sys.argv[0].

That is indeed a bit unfortunate. Misc/HISTORY tells me that this
feature was introduced in 1.5.0 and expanded to follow symlinks
repeatedly in 1.5.2. I have no idea why it was removed later.

   Bernhard

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



More information about the Python-list mailing list