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

Discussion in 'Python' started by skip@pobox.com, Nov 14, 2008.

  1. Guest

    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
     
    , Nov 14, 2008
    #1
    1. Advertising

  2. Peter Otten Guest

    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")

    http://www.python.org/doc/2.5.2/lib/module-sys.html#l2h-5167

    """
    ps1
    ps2
    Strings specifying the primary and secondary prompt of the interpreter.
    These are only defined if the interpreter is in interactive mode.
    """
     
    Peter Otten, Nov 14, 2008
    #2
    1. Advertising

  3. Guest

    Peter> hasattr(sys, "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
    "isinteractive" method or attribute.

    --
    Skip Montanaro - - http://smontanaro.dyndns.org/
     
    , Nov 14, 2008
    #3
  4. alex23 Guest

    On Nov 15, 9:48 am, 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
    > "isinteractive" method or attribute.


    You could add the following to your sitecustomize.py:

    sys.isinteractive = hasattr(sys, 'ps1')

    If you want to check for iPython as well:

    sys.isinteractive = hasattr(sys, 'ps1') or hasattr(sys,
    'ipcompleter')
     
    alex23, Nov 15, 2008
    #4
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Florian Schulze

    Check from C that Interpreter is running

    Florian Schulze, Jul 22, 2003, in forum: Python
    Replies:
    1
    Views:
    311
    Michael Hudson
    Jul 22, 2003
  2. rbt
    Replies:
    8
    Views:
    370
  3. gaurav kashyap
    Replies:
    2
    Views:
    633
    gaurav kashyap
    Oct 30, 2008
  4. gaurav kashyap
    Replies:
    3
    Views:
    702
    gaurav kashyap
    Oct 31, 2008
  5. Mel
    Replies:
    10
    Views:
    3,161
    Sailaja Appi
    Feb 13, 2009
Loading...

Share This Page