Checking for interactive shell in a Python script
This is often works well enough
import os, sysif os.isatty(sys.stdout.fileno()): ...
From this link you can use the same way and test if stdin is associated to a terminate(tty), you can do this using os.isatty(), example:
>>> os.isatty(0)True
N.B: From the same link this will fails when you invoke the command remotely via ssh, the solution given is to test if stdin is associated to a pipe.
If you already have a dependency on matplotlib, or you don't mind introducing one, you can always just call matplotlib.is_interactive()