Can I check if I'm running from the interpreter prompt?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • skip@pobox.com

    Can I check if I'm running from the interpreter prompt?

    Is there a reliable way (this is on Solaris if that matters) to tell if I'm
    running in the interactive interpreter as opposed to in a script? I think
    examining sys.argv works, but wanted to double check.

    Thx,

    Skip
  • Scott David Daniels

    #2
    Re: Can I check if I'm running from the interpreter prompt?

    skip@pobox.com wrote:
    Is there a reliable way (this is on Solaris if that matters) to tell if I'm
    running in the interactive interpreter as opposed to in a script? I think
    examining sys.argv works, but wanted to double check.
    import sys, traceback

    try:
    raise ValueError
    except ValueError:
    start = traceback.extra ct_tb(sys.exc_i nfo()[2])[-1]

    Start should show you where the program is being run from.


    --Scott David Daniels
    Scott.Daniels@A cm.Org

    Comment

    • Peter Otten

      #3
      Re: Can I check if I'm running from the interpreter prompt?

      skip@pobox.com wrote:
      Is there a reliable way (this is on Solaris if that matters) to tell if
      I'm
      running in the interactive interpreter as opposed to in a script? I think
      examining sys.argv works, but wanted to double check.
      hasattr(sys, "ps1")



      """
      ps1
      ps2
      Strings specifying the primary and secondary prompt of the interpreter.
      These are only defined if the interpreter is in interactive mode.
      """

      Comment

      • skip@pobox.com

        #4
        Re: Can I check if I'm running from the interpreter prompt?


        Peterhasattr(sy s, "ps1")

        Thanks. I wasn't aware there was a documented way to check for
        interactivity. It would have been more obvious if sys had an
        "isinteract ive" method or attribute.

        --
        Skip Montanaro - skip@pobox.com - http://smontanaro.dyndns.org/

        Comment

        • alex23

          #5
          Re: Can I check if I'm running from the interpreter prompt?

          On Nov 15, 9:48 am, s...@pobox.com wrote:
          Thanks.  I wasn't aware there was a documented way to check for
          interactivity.  
          That's more of a side-effect than the actual intent of those
          attributes, which are there to hold the interpreter prompts. But it
          does seem to be the only way.

          It might be worth noting that this -doesn't- hold true for iPython:

          IPython 0.9.1 -- An enhanced Interactive Python.
          [...]
          In [1]: import sys
          In [2]: hasattr(sys, 'ps1')
          Out[2]: False
          It would have been more obvious if sys had an
          "isinteract ive" method or attribute.
          You could add the following to your sitecustomize.p y:

          sys.isinteracti ve = hasattr(sys, 'ps1')

          If you want to check for iPython as well:

          sys.isinteracti ve = hasattr(sys, 'ps1') or hasattr(sys,
          'ipcompleter')

          Comment

          Working...