detect interactivity

Dave Angel davea at ieee.org
Tue Dec 29 14:34:20 EST 2009


Antoine Pitrou wrote:
> Le Tue, 29 Dec 2009 16:09:58 +0100, Roald de Vries a écrit :
>
>   
>> Dear all,
>>
>> Is it possible for a Python script to detect whether it is running
>> interactively? It can be useful for e.g. defining functions that are
>> only useful in interactive mode.
>>     
>
> Try the isatty() method (*) on e.g. stdin:
>
> $ python -c "import sys; print sys.stdin.isatty()"
> True
> $ echo "" | python -c "import sys; print sys.stdin.isatty()"
> False
>
>
> (*) http://docs.python.org/library/stdtypes.html#file.isatty
>
>
>   
Your test determines whether input is redirected.  But I think the OP 
was asking how to detect whether the script was being run from an 
interpreter prompt.


DaveA



More information about the Python-list mailing list