finding real $0

Thomas Wouters thomas at xs4all.net
Fri Apr 21 12:48:17 CEST 2000


On Fri, Apr 21, 2000 at 10:55:26AM +0100, Robin Becker wrote:

> Is there any pythonic way to locate the original argument zero of the
> process? I mean the location of python itself not the script. This would
> allow me to launch subscripts without having to rely on the path being
> set up correctly.

Use 'sys.executable':

centurion:~ > python
>>> import sys 
>>> sys.executable
'/usr/bin/python'

centurion:~ > ln -s /usr/bin/python penguin-on-the-telly
centurion:~ > ./penguin-on-the-telly 
>>> import sys
>>> sys.executable
'./penguin-on-the-telly'

It behaves the same way in a script (it returns the python executable
started, not the script.) Dont forget about relative paths though, if you
are going to do stuff based on sys.executable. You probably want to get the
cwd at the earliest possible moment, and translate sys.executable into an
absolute path.

-- 
Thomas Wouters <thomas at xs4all.net>

Hi! I'm a .signature virus! copy me into your .signature file to help me spread!




More information about the Python-list mailing list