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

Bengt Richter bokr at oz.net
Thu Nov 10 21:59:04 EST 2005


On Thu, 10 Nov 2005 23:29:28 +0100, "Fredrik Lundh" <fredrik at pythonware.com> wrote:

>Bengt Richter 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.)
>
>> 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]
>>  '<interactive>'
>>
>>  >>> 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



More information about the Python-list mailing list