Determine whether program was started by clicking icon or commandline

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Benjamin Hell

    Determine whether program was started by clicking icon or commandline

    Hi!

    I wonder whether there might be a way to find out how a Python
    program was started (in my case in Windows): By double clicking the
    file or by calling it on the "DOS" command line prompt.

    Background: I would like to have the program run in an "interactiv e
    mode" if double clicked, and silently in a "batch mode" when started
    otherwise.

    Any hints?

    Thank you!

    Ben
  • Atila Olah

    #2
    Re: Determine whether program was started by clicking icon or commandline

    On Nov 29, 9:51 am, Benjamin Hell <bh...@spamfenc e.netwrote:
    Hi!
    >
    I wonder whether there might be a way to find out how a Python
    program was started (in my case in Windows): By double clicking the
    file or by calling it on the "DOS" command line prompt.
    I think it's not possible (or very tricky) to do that.
    Background: I would like to have the program run in an "interactiv e
    mode" if double clicked, and silently in a "batch mode" when started
    otherwise.
    >
    Any hints?
    Why don't you just create a desktop icon that calls "myprog.py --
    interactive-mode" and then check in sys.argv for the actual argument?
    Or, if you prefer not to use arguments, you could just change the
    working directory of the shortcut and then check os.getcwd()? Though
    it's a weird thing to do...

    Hope I could.
    aatiis

    Comment

    • Roger Miller

      #3
      Re: Determine whether program was started by clicking icon or commandline

      On Nov 28, 10:51 pm, Benjamin Hell <bh...@spamfenc e.netwrote:
      Hi!
      >
      I wonder whether there might be a way to find out how a Python
      program was started (in my case in Windows): By double clicking the
      file or by calling it on the "DOS" command line prompt.
      >
      Background: I would like to have the program run in an "interactiv e
      mode" if double clicked, and silently in a "batch mode" when started
      otherwise.
      I'm not sure whether this applies to your situation, but often
      programs
      started by clicking an icon are run by pythonw, but when started from
      the command line are run by python. If this is the case
      sys.stdin.filen o()
      will return -1 in the former case and 0 in the latter.

      Comment

      • Benjamin Hell

        #4
        Re: Determine whether program was started by clicking icon or commandline

        Roger Miller wrote:
        >I wonder whether there might be a way to find out how a Python
        >program was started (in my case in Windows): By double clicking
        >the file or by calling it on the "DOS" command line prompt.
        >
        I'm not sure whether this applies to your situation, but often
        programs started by clicking an icon are run by pythonw, but when
        started from the command line are run by python. If this is the
        case sys.stdin.filen o() will return -1 in the former case and 0
        in the latter.
        Nice idea, but this...

        import sys
        print sys.stdin.filen o()
        raw_input("Pres s any key to exit")

        ....
        always prints "0" in my case ("DOS", double click in Windows
        Explorer, Cygwin shell).

        Thanks anyways!

        Ben

        Comment

        • Adonis Vargas

          #5
          Re: Determine whether program was started by clicking icon or commandline

          Roger Miller wrote:
          On Nov 28, 10:51 pm, Benjamin Hell <bh...@spamfenc e.netwrote:
          >Hi!
          >>
          >I wonder whether there might be a way to find out how a Python
          >program was started (in my case in Windows): By double clicking the
          >file or by calling it on the "DOS" command line prompt.
          >>
          >Background: I would like to have the program run in an "interactiv e
          >mode" if double clicked, and silently in a "batch mode" when started
          >otherwise.
          >
          I'm not sure whether this applies to your situation, but often
          programs
          started by clicking an icon are run by pythonw, but when started from
          the command line are run by python. If this is the case
          sys.stdin.filen o()
          will return -1 in the former case and 0 in the latter.
          >
          No, when it gets executed by pythonw the application has an extension of
          ..pyw and .py files are run by the regular python executable. This the
          default behavior, unless for some odd reason you modified this through
          the registry or through explorer to do otherwise.

          Adonis Vargas

          Comment

          Working...