1

Currently, I'm running this on Windows:

args = ['start', windowname, 'python', '-i', myscript]
subprocess.Popen(args, shell=True)

As you can see, I launch a subprocess running myscript in python's interactive mode. In my case, this means that once the script exits, regardless if it errors out or successfully completes, the window/shell stays open. However, I'd like it so that when myscript errors out, the window will stay open, and when myscript runs successfully, the window will close.

The reason I'm doing this is because I want to see the errors and the output leading up to the errors -- I'd prefer to not use some form of logging because it's easier for me to visually see the windows and outputs.

I don't think I can check the returncode because the process I'm interacting with is start/cmd, rather than python. Please correct me if I'm wrong!

Thanks!

jaka
  • 155
  • 1
  • 2
  • 10
  • What exactly are you trying to run? A script from within a script? Can't you call `python myscript` from windows command prompt? Deleted my answer because I realized I have no idea what you are trying to do. – msvalkon Mar 28 '13 at 07:38
  • Basically, from within my main python script, I'm launching many other subprocesses that runs `myscript` from python. The reason I'm doing this is because I want each `myscript` to have its own window. Each `myscript` has streaming output similar to, say, wget: `xxx/yyy KB downloaded (xx.xx%)` and I want them to be in their own respective window for ease of administration. I'm calling `start` instead of `python` is because I want to also name each window, as seen from `['start', windowname,...` – jaka Mar 28 '13 at 07:45
  • Every script run by `python.exe` has its own console window by default (as opposed to script run by `pythonw.exe`). Also, you can set window's title directly from within Python script (see http://stackoverflow.com/a/12626424/95735) so there's no need to use `start`. – Piotr Dobrogost May 15 '13 at 21:40

1 Answers1

0

I think the easiest way would be to wrap the whole thing in a small batch script that checks for python's return code (presumably set by you by calling sys.exit() with an appropriate return code).

This stackoverflow question covers how to get the return code (apparently it's in %errorlevel%), and you can keep the cmd window open by executing pause in a batch script.

Community
  • 1
  • 1
wds
  • 31,873
  • 11
  • 59
  • 84
  • thanks for the response -- it works as intended, but what if i'd like to use `python -i` instead of just `python`? – jaka Mar 28 '13 at 08:25
  • @jaka simply `sys.exit(0)` should work to exit in case you didn't detect any errors (it also ends the interactive interpreter). I guess I don't really see the problem? – wds Mar 28 '13 at 09:36