8

I use the Windows version of Python. I have a Python script using Pyside (nothing complicated, a kind of "hello world").

When I click on my script file or if I launch it from a command line, it executes perfectly and I have a GUI appearing.

However, I would like to avoid having a GUI if the script is launched from a textual terminal (cmd.exe, cygwin, ...). A kind of script which automatically knows if it should have a GUI output or a textual output.

Is there an easy and simple way to do that? I want to be able to do that with the Windows version of Python (not the one coming with Cygwin packages).

An obvious way would be to add a kind of "--no-gui" parameter when I launch the script from a textual terminal, but I wonder if Python (or some Python libraries) already provide tools for that.

Moreover I have an SSH server (Cygwin-base) on this computer, I can execute the script at distance but no GUI appear (of course) and I have no error message. It is a case where it is very interesting to know if the script failed because of the lack of Windows graphical support or if the script should adapt its output for a textual terminal.

Vincent Hiribarren
  • 5,254
  • 2
  • 41
  • 65
  • 2
    Its not a duplicate since this question is about Windows, but the discussion at http://stackoverflow.com/questions/3818511/how-to-tell-if-python-script-is-being-run-in-a-terminal-or-via-gui might be helpful. – Aaron Dufour Apr 12 '12 at 08:38
  • 1
    more discussion is also here: http://stackoverflow.com/questions/1285024/how-can-i-check-to-see-if-a-python-script-was-started-interactively – Jiri Apr 12 '12 at 13:28

1 Answers1

2

I know that you can run file as .py file or as .pyw file. The second option is used for GUI applications and it does not open the console window. To distinguish these to two cases you can check isatty method of sys.stdout.

import sys
if sys.stdout.isatty():
    # .py file is running, with console window
    pass
else:
    # .pyw file is running, no console
    pass

EDIT:

  • I tried to run that with putty+ssh on linux box - it returns True.
  • I tried to use msys bash shell on windows box - it returns True (.py file)
  • I tried to use cygwin bash shell with cygwin python - it returns True (.py file)
  • Unfortunately, I have no possibility to try putty + windows cygwin ssh server.
Jiri
  • 16,425
  • 6
  • 52
  • 68
  • You can also use `pythonw.exe` to run without console. – bereal Apr 12 '12 at 08:33
  • Interesting, but does not work in all cases. From cmd.exe and when clicking on the file, isatty() is True for a .py and False for a .pyw, so your idea works well. From cygwin and a SSH session (Putty and ssh for cygwin) , isatty() is False (for reminding, I use the Windows version of Python, not the Cygwin one). Or do I miss something? – Vincent Hiribarren Apr 12 '12 at 08:34
  • I am really sorry, but I tested once again, and contrary to what your edit says, it does not work with the Cygwin bash shell. Which Python version did you use? I am specifically talking about the Windows version of Python, not the one coming with the Cygwin packages (I agree it returns True for it, but the idea is to use Python for Windows). – Vincent Hiribarren Apr 12 '12 at 11:04
  • Ok, sorry, it was not cygwin bash, it was msys bash (GNU bash, version 3.1.0(3)-release (i686-pc-msys)) + windows python 2.7.2 – Jiri Apr 12 '12 at 12:52
  • ok, now it seems to me very wrong to use windows cpython and cygwin together. At first, you cannot use cygwin paths in cpython, and console seems to be also broken. I tried and I could not even get the console running by executing /cygdrive/c/Python27/python.exe Anyway I try your case and it returns False as you say, but I think that you should use cygwin python or standardn non-cygwin console with windows python. – Jiri Apr 12 '12 at 13:21
  • Try with -i to have the interactive shell, and with -u to see the output. Sorry but I cannot do what you say. For instance, I use PyBluez for bluetooth connection but it does not install well with Cygwin. It does with Windows Python. Moreover if I use Cygwin, the trick with *.pyw and pythonw.exe does not work any more since pythonw.exe comes with the Windows Python distribution. – Vincent Hiribarren Apr 12 '12 at 13:37