Detecting -i in a script

Chris Rebert clp2 at rebertia.com
Tue Apr 14 03:22:14 CEST 2009


On Mon, Apr 13, 2009 at 6:10 PM, Static Vagabond <static at viralmind.org> wrote:
> Chris Rebert wrote:
>>>
>>> Marek Szuba wrote:
>>>>
>>>> On 2009-04-13, Chris Rebert <clp2 at rebertia.com> 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 <static at viralmind.org>
>> 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 at morpheus ~ $ cat foo.py
>> from sys import argv
>> print argv
>> chris at 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



More information about the Python-list mailing list