Prevent a console app from closing when not invoked from an existing terminal? <p>There are many variants on this kind of question. However I am specifically after a way to prevent a console application in Python from closing when it is not invoked from a terminal (or other console, as it may be called on Windows). An example where this could occur is double clicking a <code>.py</code> file from the Windows explorer.</p> <p>Typically I use something like the following code snippet, but it has the unfortunate side effect of operating even if the application is invoked from an existing terminal:</p> <pre><code>def press_any_key(): if os.name == "nt": os.system("pause") atexit.register(press_any_key) </code></pre> <p>It's also making the assumption that all Windows users are invoking the application from the Windows "shell", and that only Windows users can execute the program from a location other than an existing terminal.</p> <p>Is there a (preferably cross platform) way to detect if my application has been invoked from a terminal, and/or whether it is necessary to provide a "press any key..." functionality for the currently running instance? Note that resorting to batch, bash or any other "wrapper process" workarounds are highly undesirable.</p> <h2>Update0</h2> <p>Using <a href="http://stackoverflow.com/questions/2258771/prevent-a-console-app-from-closing-when-not-executed-from-a-terminal/2258980#2258980">Alex Martelli's</a> answer below, I've produced this function:</p> <pre><code>def register_pause_before_closing_console(): import atexit, os if os.name == 'nt': from win32api import GetConsoleTitle if not GetConsoleTitle().startswith(os.environ["COMSPEC"]): atexit.register(lambda: os.system("pause")) if __name__ == '__main__': register_pause_before_closing_console() </code></pre> <p>If other suitable answers arise, I'll append more code for other platforms and desktop environments.</p> <h2>Update1</h2> <p>In the vein of using <a href="http://sourceforge.net/projects/pywin32/" rel="nofollow noreferrer">pywin32</a>, I've produced <strong>this</strong> function, which improves on the one above, using the accepted answer. The commented out code is an alternative implementation as originating in Update0. If using pywin32 is not an option, follow the link in the <a href="http://stackoverflow.com/questions/2258771/prevent-a-console-app-from-closing-when-not-invoked-from-an-existing-terminal/2261219#2261219">accepted answer</a>. Pause or getch() to taste.</p> <pre><code>def _current_process_owns_console(): #import os, win32api #return not win32api.GetConsoleTitle().startswith(os.environ["COMSPEC"]) import win32console, win32process conswnd = win32console.GetConsoleWindow() wndpid = win32process.GetWindowThreadProcessId(conswnd)[1] curpid = win32process.GetCurrentProcessId() return curpid == wndpid def register_pause_before_closing_console(): import atexit, os, pdb if os.name == 'nt': if _current_process_owns_console(): atexit.register(lambda: os.system("pause")) if __name__ == '__main__': register_pause_before_closing_console() </code></pre>