473,322 Members | 1,614 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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

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

Nov 10 '05 #1
9 3909
Swaroop C H wrote:
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


import sys
print sys.argv[0]

regards
Steve
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 10 '05 #2
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.

</F>

Nov 10 '05 #3
Fredrik Lundh wrote:
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
--
Steve Holden +44 150 684 7255 +1 800 494 3119
Holden Web LLC www.holdenweb.com
PyCon TX 2006 www.python.org/pycon/

Nov 10 '05 #4
Thanks Fredrik for the info.

It was not a must-have, I was just curious if it was possible.

Thanks,
Swaroop
www.SwaroopCH.info

Nov 10 '05 #5
Swaroop C H wrote:
Thanks Fredrik for the info.

It was not a must-have, I was just curious if it was possible.


it should be noted that some systems have a setproctitle() function, but the design
of that API comes with its own can of quality worms:

http://www.cert.org/advisories/CA-2000-13.html

</F>

Nov 10 '05 #6
On Thu, 10 Nov 2005 08:06:27 +0100, "Fredrik Lundh" <fr*****@pythonware.com> wrote:
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.

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

Regards,
Bengt Richter
Nov 10 '05 #7
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

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

</F>

Nov 10 '05 #8
On Thu, 10 Nov 2005 23:29:28 +0100, "Fredrik Lundh" <fr*****@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
Nov 11 '05 #9
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

Nov 11 '05 #10

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Will Stuyvesant | last post by:
I never used the popen or popen2 libraries but it is my understanding that they can capture the output of console based programs. Is it also possible to send keystrokes to console base programs? ...
3
by: Ricardo | last post by:
Hi, my name is Ricardo and i'm learning to program in Python with version 2.3.3, i'm also using the latest version of PythonWin(163) but i'm trying to do it with a book (Written in Portuguese) that...
4
by: Charles Collins | last post by:
Is there a way to set the program name for a connection so that it is a bit more descriptive than ".Net SqlClient Data Provider"? Thanks, Charles
13
by: squash | last post by:
I am a little annoyed at why such a simple program in Perl is causing so much difficulty for python, i.e: $a += 200000 * 140000; print $a;
2
by: diffuser78 | last post by:
I want to write a simple clinet/server program that does the following. On Windows, the program listens to the request from linux box On Linux Box, client program send server a shutdown request...
1
by: GnanaKumar | last post by:
hi, Can any one explain me how to execute COBOL program using Python scripts... do i need any third party tool to do this.. can you pls help me out regards, Gnana Kumar
0
by: Dr. Colombes | last post by:
I've used a very nice Keystroke Logger Python shareware program (EVDEV.py), written by Micah Dowty. But I couldn't find a dedicated ASCII Character Logger program. Does anyone know of an ASCII...
24
by: Peter Michaux | last post by:
I have a Perl script that I want to run as a set-user-ID program. Many OSes don't allow scripts run as set-user-ID. To make this script portable, it seems I need to write a C wrapper program that...
2
by: raulbolanos | last post by:
Hello, I made a Setup file and everything is in order, but not the Program Name that is been showed at the Add or Remove Program Section... I already change the properties from the assembly...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.