How to get path of a .py script

David Necas (Yeti) yeti at physics.muni.cz
Wed Dec 11 15:32:47 EST 2002


On Wed, Dec 11, 2002 at 09:11:59PM +0100, Bernhard Herzog wrote:
> "David Necas (Yeti)" <yeti at physics.muni.cz> writes:
> 
> > 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).

OK, when desktop link it's something more like `shortcut' in
MS-Windows, than it's completely another matter.

While a path containing ../ is a good as any other,
resolving the ../ usually makes one more feel comfortable
(and safer), that's why I'd suggest still try to resolve
sys.path[0] before use.

> 
> > 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.

After I posted it, I realized this is a strong argument
against using sys.path[0] for anything, because it allows
a nasty symlink attack.

Moreover, it's just a directory, not the link itself, thus
it's impossible to correctly resolve it without using some
other information.  I can make it contain anything I want (I
need write permissions in the directory which will appear
there) by deliberately symlinking the script.

Yeti





More information about the Python-list mailing list