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

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Swaroop C H

    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?
    [color=blue]
    >From `man perlvar`:[/color]

    $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


  • Steve Holden

    #2
    Re: How to set program name in Python? ($0 in Perl)

    Swaroop C H wrote:[color=blue]
    > Hi,
    >
    > Is there a way to set the program name in Python, similar to $0 in
    > Perl?
    >[color=green]
    >>From `man perlvar`:[/color]
    >
    > $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
    >[/color]

    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/

    Comment

    • Fredrik Lundh

      #3
      Re: How to set program name in Python? ($0 in Perl)

      Steve Holden wrote:
      [color=blue][color=green]
      > > Is there a way to set the program name in Python, similar to $0 in
      > > Perl?
      > >[color=darkred]
      > >>From `man perlvar`:[/color]
      > >
      > > $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.)[/color][/color]
      [color=blue]
      > import sys
      > print sys.argv[0][/color]

      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>



      Comment

      • Steve Holden

        #4
        Re: How to set program name in Python? ($0 in Perl)

        Fredrik Lundh wrote:[color=blue]
        > Steve Holden wrote:
        >
        >[color=green][color=darkred]
        >>>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.)[/color][/color]
        >
        >[color=green]
        >>import sys
        >>print sys.argv[0][/color]
        >
        >
        > 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.
        >[/color]
        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/

        Comment

        • Swaroop C H

          #5
          Re: How to set program name in Python? ($0 in Perl)

          Thanks Fredrik for the info.

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

          Thanks,
          Swaroop


          Comment

          • Fredrik Lundh

            #6
            Re: How to set program name in Python? ($0 in Perl)

            Swaroop C H wrote:
            [color=blue]
            > Thanks Fredrik for the info.
            >
            > It was not a must-have, I was just curious if it was possible.[/color]

            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:

            This document contains the CERT advisories from 2000.


            </F>



            Comment

            • Bengt Richter

              #7
              Re: How to set program name in Python? ($0 in Perl)

              On Thu, 10 Nov 2005 08:06:27 +0100, "Fredrik Lundh" <fredrik@python ware.com> wrote:
              [color=blue]
              >Steve Holden wrote:
              >[color=green][color=darkred]
              >> > 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.)[/color][/color]
              >[color=green]
              >> import sys
              >> print sys.argv[0][/color]
              >
              >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.
              >[/color]
              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?
              [color=blue][color=green][color=darkred]
              >>> import sys
              >>> sys.argv[0][/color][/color][/color]
              ''[color=blue][color=green][color=darkred]
              >>> sys.argv[0] = '<interactive> '
              >>> sys.argv[0][/color][/color][/color]
              '<interactive> '
              [color=blue][color=green][color=darkred]
              >>> type(sys.argv)[/color][/color][/color]
              <type 'list'>

              Regards,
              Bengt Richter

              Comment

              • Fredrik Lundh

                #8
                Re: How to set program name in Python? ($0 in Perl)

                Bengt Richter wrote:
                [color=blue][color=green][color=darkred]
                > >> > 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.)[/color][/color][/color]
                [color=blue]
                > 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?
                >[color=green][color=darkred]
                > >>> import sys
                > >>> sys.argv[0][/color][/color]
                > ''[color=green][color=darkred]
                > >>> sys.argv[0] = '<interactive> '
                > >>> sys.argv[0][/color][/color]
                > '<interactive> '
                >[color=green][color=darkred]
                > >>> type(sys.argv)[/color][/color]
                > <type 'list'>[/color]

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



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

                </F>



                Comment

                • Bengt Richter

                  #9
                  Re: How to set program name in Python? ($0 in Perl)

                  On Thu, 10 Nov 2005 23:29:28 +0100, "Fredrik Lundh" <fredrik@python ware.com> wrote:
                  [color=blue]
                  >Bengt Richter wrote:
                  >[color=green][color=darkred]
                  >> >> > 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.)[/color][/color]
                  >[color=green]
                  >> 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?
                  >>[color=darkred]
                  >> >>> import sys
                  >> >>> sys.argv[0][/color]
                  >> ''[color=darkred]
                  >> >>> sys.argv[0] = '<interactive> '
                  >> >>> sys.argv[0][/color]
                  >> '<interactive> '
                  >>[color=darkred]
                  >> >>> type(sys.argv)[/color]
                  >> <type 'list'>[/color]
                  >
                  >given that Swaroop has written a nice book about Python, I somehow
                  >suspect that he knows how sys.argv works:
                  >
                  > http://tinyurl.com/9s7bz
                  >[/color]
                  Sorry, I wasn't familiar with that (or Swaroop ;-)
                  [color=blue]
                  >or are you saying that "ps" looks inside sys.argv on your machine?
                  >[/color]
                  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>log line 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

                  Comment

                  • jmdeschamps@gmail.com

                    #10
                    Re: How to set program name in Python? ($0 in Perl)

                    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 testChangingNam e(appname):
                    hopedfornameis = appname
                    mylongnameis = sys.argv[0]
                    mynameis = os.path.basenam e(mylongnameis)
                    basenameis = os.path.dirname (mylongnameis)
                    if mynameis != hopedfornameis:
                    shutil.copyfile (mynameis, basenameis+"/"+hopedfornamei s)

                    os.spawnv(os.P_ NOWAIT,"c:/python23/python.exe",('c :/python23/python.exe',hop edfornameis))
                    sys.exit()

                    if __name__ == "__main__":
                    print sys.argv[0]
                    testChangingNam e("testNameIsCh anged.py")
                    s=raw_input("Al l's well!")
                    s=raw_input("No w 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

                    Comment

                    Working...