Detecting -i in a script

Discussion in 'Python' started by Marek Szuba, Apr 13, 2009.

  1. Marek Szuba

    Marek Szuba Guest

    Hello there,

    Is there any way of detecting in a script whether the interpreter
    session running it has been launched with the -i option? My Google fu
    has failed me on this subject... Thanks in advance.

    --
    MS
     
    Marek Szuba, Apr 13, 2009
    #1
    1. Advertising

  2. Marek Szuba

    Chris Rebert Guest

    On Mon, Apr 13, 2009 at 12:09 PM, Marek Szuba <> wrote:
    > Hello there,
    >
    > Is there any way of detecting in a script whether the interpreter
    > session running it has been launched with the -i option? My Google fu
    > has failed me on this subject... Thanks in advance.


    The sys.flags.interactive bool.

    Details: http://docs.python.org/library/sys.html#sys.flags

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Apr 13, 2009
    #2
    1. Advertising

  3. Marek Szuba

    Marek Szuba Guest

    On 2009-04-13, Chris Rebert <> wrote:

    > The sys.flags.interactive bool.
    >
    > Details: http://docs.python.org/library/sys.html#sys.flags

    Hmm, "New in version 2.6"... Are you aware of any way of extracting
    this information in older versions of Python? My code needs to be
    2.3-compatible.

    --
    MS
     
    Marek Szuba, Apr 13, 2009
    #3
  4. Marek Szuba

    Chris Rebert Guest

    > Marek Szuba wrote:
    >>
    >> On 2009-04-13, Chris Rebert <> wrote:
    >>
    >>> The sys.flags.interactive bool.
    >>>
    >>> Details: http://docs.python.org/library/sys.html#sys.flags

    >>
    >> Hmm, "New in version 2.6"... Are you aware of any way of extracting
    >> this information in older versions of Python? My code needs to be
    >> 2.3-compatible.


    On Mon, Apr 13, 2009 at 5:32 PM, Static Vagabond <> wrote:
    > I think getopt will help you achieve what you need.
    > http://docs.python.org/library/getopt.html


    A. Please don't top-post. It makes following the conversation more
    difficult by presenting it out of chronological order.

    B. -i is an option to the Python interpreter *itself*, not the Python
    script, and so gets gobbled up before the script even starts, thus
    `getopt` won't work. Observe:

    chris@morpheus ~ $ cat foo.py
    from sys import argv
    print argv
    chris@morpheus ~ $ python -i foo.py
    ['foo.py']
    >>>


    Cheers,
    Chris
    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Apr 14, 2009
    #4
  5. Marek Szuba

    Chris Rebert Guest

    On Mon, Apr 13, 2009 at 6:10 PM, Static Vagabond <> wrote:
    > Chris Rebert wrote:
    >>>
    >>> Marek Szuba wrote:
    >>>>
    >>>> On 2009-04-13, Chris Rebert <> wrote:
    >>>>
    >>>>> The sys.flags.interactive bool.
    >>>>>
    >>>>> Details: http://docs.python.org/library/sys.html#sys.flags
    >>>>
    >>>> Hmm, "New in version 2.6"... Are you aware of any way of extracting
    >>>> this information in older versions of Python? My code needs to be
    >>>> 2.3-compatible.

    >>
    >> On Mon, Apr 13, 2009 at 5:32 PM, Static Vagabond <>
    >> wrote:
    >>>
    >>> I think getopt will help you achieve what you need.
    >>> http://docs.python.org/library/getopt.html

    >>
    >> A. Please don't top-post. It makes following the conversation more
    >> difficult by presenting it out of chronological order.
    >>
    >> B. -i is an option to the Python interpreter *itself*, not the Python
    >> script, and so gets gobbled up before the script even starts, thus
    >> `getopt` won't work. Observe:
    >>
    >> chris@morpheus ~ $ cat foo.py
    >> from sys import argv
    >> print argv
    >> chris@morpheus ~ $ python -i foo.py
    >> ['foo.py']
    >>
    >> Cheers,
    >> Chris

    >
    >
    > Thanks for the tip Chris, been away from the newsgroups for a while and bad
    > habits seem to have crept in.  All in all, a none-to-useful first post to
    > comp.lang.python, here's hoping for a brighter future.
    >
    > Can I presume the lack of a solution in your response means, essentially,
    > prior to 2.6 there's no way of reading from the script that the interpreter
    > has been launched in interactive mode?


    I'm not a (C)Python implementor, so I can't definitively answer that.
    The fact that this functionality was added at all does, however, seem
    to strongly imply that that is the case.
    FWIW, I've scanned over the `sys` module, which would be the obvious
    place to put such a feature or its rudiments, and can't find anything
    else of use for the task.

    Cheers,
    Chris

    --
    I have a blog:
    http://blog.rebertia.com
     
    Chris Rebert, Apr 14, 2009
    #5
    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. =?Utf-8?B?U2FtYmE=?=

    Detecting Script enabled or not in Client Browsers

    =?Utf-8?B?U2FtYmE=?=, Jul 29, 2005, in forum: ASP .Net
    Replies:
    4
    Views:
    348
    Rob Amos
    Jul 29, 2005
  2. Replies:
    0
    Views:
    843
  3. the idiot
    Replies:
    33
    Views:
    1,089
    the idiot
    Mar 4, 2005
  4. Kevin Spencer

    Detecting script enabled

    Kevin Spencer, Sep 20, 2006, in forum: ASP .Net
    Replies:
    2
    Views:
    414
    Barry
    Sep 21, 2006
  5. Hal
    Replies:
    4
    Views:
    118
Loading...

Share This Page