7

is there a way to find out from within a python program if it was started in a terminal or e.g. in a batch engine like sun grid engine?

the idea is to decide on printing some progress bars and other ascii-interactive stuff, or not.

thanks!

p.

raam86
  • 6,785
  • 2
  • 31
  • 46
  • Welcome to the site, Pavel. If any of the responses below solve your problem, you may wish to officially accept the one that you feel provided the best answer; the answerer gets a few extra points, and it helps future visitors to the identify the best solution. – Jarret Hardie Jun 08 '09 at 23:31
  • by the way, what about running `printenv` in a job and see which environment variables are set by Sun Grid Engine (such as the job number) which are not set during a typical interactive shell ? – Andre Holzner Aug 27 '11 at 09:06
  • Possible duplicate of [Checking for interactive shell in a Python script](https://stackoverflow.com/questions/6108330/checking-for-interactive-shell-in-a-python-script) – ideasman42 Aug 29 '17 at 06:05

4 Answers4

12

The standard way is isatty().

import sys
if sys.stdout.isatty():
    print("Interactive")
else:
    print("Non-interactive")
Dietrich Epp
  • 205,541
  • 37
  • 345
  • 415
  • thank you very much, works just as intended! exactly what i looked for. p. –  Jun 08 '09 at 23:12
6

You can use os.getppid() to find out the process id for the parent-process of this one, and then use that process id to determine which program that process is running. More usefully, you could use sys.stdout.isatty() -- that doesn't answer your title question but appears to better solve the actual problem you explain (if you're running under a shell but your output is piped to some other process or redirected to a file you probably don't want to emit "interactive stuff" on it either).

Alex Martelli
  • 854,459
  • 170
  • 1,222
  • 1,395
3

Slightly shorter:

import sys

sys.stdout.isatty()
Andy Grover
  • 608
  • 3
  • 6
2

I have found the following to work on both Linux and Windows, in both the normal Python interpreter and IPython (though I can't say about IronPython):

isInteractive = hasattr(sys, 'ps1') or hasattr(sys, 'ipcompleter')

However, note that when using ipython, if the file is specified as a command-line argument it will run before the interpreter becomes interactive. See what I mean below:

C:\>cat C:\demo.py
import sys, os

# ps1=python shell; ipcompleter=ipython shell
isInteractive = hasattr(sys, 'ps1') or hasattr(sys, 'ipcompleter')
print isInteractive and "This is interactive" or "Automated"

C:\>python c:\demo.py
Automated

C:\>python
>>> execfile('C:/demo.py')
This is interactive

C:\>ipython C:\demo.py
Automated       # NOTE! Then ipython continues to start up...

IPython 0.9.1 -- An enhanced Interactive Python.
?         -> Introduction and overview of IPython's features.
%quickref -> Quick reference.
help      -> Python's own help system.
object?   -> Details about 'object'. ?object also works, ?? prints more.

In [2]: run C:/demo.py
This is interactive    # NOTE!

HTH

Roark Fan
  • 672
  • 8
  • 9