How to set program name in Python? ($0 in Perl)

S

Swaroop C H

Hi,

Is there a way to set the program name in Python, similar to $0 in
Perl?
From `man perlvar`:

$0 Contains the name of the program being executed. On some oper-
ating systems assigning to "$0" modifies the argument
area that
the ps program sees. This is more useful as a way of
indicat-
ing the current program state than it is for hiding the
program
you're running. (Mnemonic: same as sh and ksh.)



Thanks!
Swaroop
www.swaroopch.info
 
S

Steve Holden

Swaroop said:
Hi,

Is there a way to set the program name in Python, similar to $0 in
Perl?


$0 Contains the name of the program being executed. On some oper-
ating systems assigning to "$0" modifies the argument
area that
the ps program sees. This is more useful as a way of
indicat-
ing the current program state than it is for hiding the
program
you're running. (Mnemonic: same as sh and ksh.)



Thanks!
Swaroop
www.swaroopch.info

import sys
print sys.argv[0]

regards
Steve
 
F

Fredrik Lundh

import sys
print sys.argv[0]

that *gets* the name, but assigning to sys.argv[0] doesn't *set* it (afaik).

setting the name involves overwriting the C level argv array, several large
buckets of worms, and huge portability issues, and is thus better left to non-
standard extensions.

</F>
 
S

Steve Holden

Fredrik said:
Steve Holden wrote:

Is there a way to set the program name in Python, similar to $0 in
Perl?

From `man perlvar`:

$0 Contains the name of the program being executed. On some oper-
ating systems assigning to "$0" modifies the argument
area that
the ps program sees. This is more useful as a way of
indicat-
ing the current program state than it is for hiding the
program
you're running. (Mnemonic: same as sh and ksh.)

import sys
print sys.argv[0]


that *gets* the name, but assigning to sys.argv[0] doesn't *set* it (afaik).

setting the name involves overwriting the C level argv array, several large
buckets of worms, and huge portability issues, and is thus better left to non-
standard extensions.
Indeed. My bad.

regards
Steve
 
B

Bengt Richter

import sys
print sys.argv[0]

that *gets* the name, but assigning to sys.argv[0] doesn't *set* it (afaik).

setting the name involves overwriting the C level argv array, several large
buckets of worms, and huge portability issues, and is thus better left to non-
standard extensions.
OTOH, if the intent is just to set a value for subsequent getting by way of
sys.argv[0], isn't sys.argv an ordinary list?
>>> import sys
>>> sys.argv[0] ''
>>> sys.argv[0] = '<interactive>'
>>> sys.argv[0]
' said:
>>> type(sys.argv)
<type 'list'>

Regards,
Bengt Richter
 
F

Fredrik Lundh

OTOH, if the intent is just to set a value for subsequent getting by way of
sys.argv[0], isn't sys.argv an ordinary list?
import sys
sys.argv[0] ''
sys.argv[0] = '<interactive>'
sys.argv[0]
' said:
type(sys.argv)
<type 'list'>

given that Swaroop has written a nice book about Python, I somehow
suspect that he knows how sys.argv works:

http://tinyurl.com/9s7bz

or are you saying that "ps" looks inside sys.argv on your machine?

</F>
 
B

Bengt Richter

OTOH, if the intent is just to set a value for subsequent getting by way of
sys.argv[0], isn't sys.argv an ordinary list?
import sys
sys.argv[0] ''
sys.argv[0] = '<interactive>'
sys.argv[0]
' said:
type(sys.argv)
<type 'list'>

given that Swaroop has written a nice book about Python, I somehow
suspect that he knows how sys.argv works:

http://tinyurl.com/9s7bz
Sorry, I wasn't familiar with that (or Swaroop ;-)
or are you saying that "ps" looks inside sys.argv on your machine?
Nope. Hm, I wrote a little C a few years ago utility that prints a date-time prefix
and the raw command line string but skipping arg 0. It uses win32's GetCommandLine,
which returns a string pointer.

[18:58] C:\pywk\clp>logline arg1 arg2 note preserved spaces.
20051110 18:58:24 arg1 arg2 note preserved spaces.

Maybe ctypes could be used to get the pointer and carefully poke in a mod. But I don't know if
the cmd line string is within a static fixed size array so you could lengthen it, or what.
Or have they opened the source for that? Anyway, I don't know if that is where ps (pstat on my NT box)
looks. Not handy to experiment ATM ;-)

Regards,
Bengt Richter
 
J

jmdeschamps

This is not directly what the OP wanted in regards to Perl, but to see
what one could do if one needed to change the name of the running
program, I wrote this:
## START PROGRAM
import sys
import os.path
import shutil
import os

def testChangingName(appname):
hopedfornameis = appname
mylongnameis = sys.argv[0]
mynameis = os.path.basename(mylongnameis)
basenameis = os.path.dirname(mylongnameis)
if mynameis != hopedfornameis:
shutil.copyfile(mynameis, basenameis+"/"+hopedfornameis)

os.spawnv(os.P_NOWAIT,"c:/python23/python.exe",('c:/python23/python.exe',hopedfornameis))
sys.exit()

if __name__ == "__main__":
print sys.argv[0]
testChangingName("testNameIsChanged.py")
s=raw_input("All's well!")
s=raw_input("Now do something useful")

## END PROGRAM

Since I don't know the circumstance in which the OP wanted to change
the name, I really don't know what caveats one should look out for
here.
I would be curious to know when such a function could come in handy.

Jean-Marc
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,780
Messages
2,569,608
Members
45,250
Latest member
Charlesreero

Latest Threads

Top