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
First, an attempt to disuade you from clever hacks. It's perfectly appropriate to have a seperate shortcut designed to be run from Explorer that does slightly different things (like holding the console open) from the script to be used from the commandline. As Alex has already pointed out, this is not an issue on nix, and the right thing to do there is always exit cleanly or your users will complain.
If you still want a workaround, here's code to detect when the console needs to be prevented from closing that's reasonably clean. Requires Windows 2000 or later, the logic is contained in this function:
def owns_console():
wnd = GetConsoleWindow()
if wnd is None:
return False
return GetCurrentProcessId() == GetWindowThreadProcessId(wnd)
Basically, it gets the PIDs of the process that owns the console Python is using, and of our process. If they are the same, then when we exit the console will go away, so it needs to be held open. If they are different, or if there's no console attached, Python should exit normally.