33

I have successfully run several Python scripts, calling them from a base script using the subprocess module:

subprocess.popen([sys.executable, 'script.py'], shell=True)

However, each of these scripts executes some simulations (.exe files from a C++ application) that generate some output to the shell. All these outputs are written to the base shell from where I've launched those scripts. I'd like to generate a new shell for each script. I've tried to generate new shells using the shell=True attribute when calling subprocess.call (also tried with popen), but it doesn't work.

How do I get a new shell for each process generated with the subprocess.call?

I was reading the documentation about stdin and stdout as suggested by Spencer and found a flag the solved the problem: subprocess.CREATE_NEW_CONSOLE. Maybe redirecting the pipes does the job too, but this seems to be the simplest solution (at least for this specific problem). I've just tested it and worked perfectly:

subprocess.popen([sys.executable, 'script.py'], creationflags = subprocess.CREATE_NEW_CONSOLE)
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
emiguel
  • 373
  • 1
  • 3
  • 9
  • Do you mean you want to run the scripts in different terminal windows? If so, what terminal program are you using? – unutbu Jun 24 '11 at 15:07
  • @unutbu: yes, I want to run each script in a different terminal window. I'm using the basic Window's terminal (just running cmd) – emiguel Jun 24 '11 at 15:48
  • @Eder: I don't know much about Windows. Does this help: http://stackoverflow.com/questions/303838/create-a-new-cmd-exe-window-from-within-another-cmd-exe-prompt ? (i.e. `start cmd.exe` or `start python script.py` ... ) – unutbu Jun 24 '11 at 15:58

3 Answers3

50

To open in a different console, do (tested on Windows 7 / Python 3):

from sys import executable
from subprocess import Popen, CREATE_NEW_CONSOLE

Popen([executable, 'script.py'], creationflags=CREATE_NEW_CONSOLE)

input('Enter to exit from this launcher script...')
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
  • 9
    Python 3 uses input instead of raw_input. – Brōtsyorfuzthrāx May 04 '14 at 00:44
  • 9
    Thanks for this answer, unfortunately CREATE_NEW_CONSOLE does not seem to work for python 2.7. Any ways around this? –  Apr 14 '16 at 13:16
  • 5
    Unfortunately, `CREATE_NEW_CONSOLE` can only be used for Windows OS. ([Source](https://stackoverflow.com/questions/44080756/python-import-from-subprocess-fails)) – Gokul NC Jun 02 '18 at 12:50
  • what if script.py has some args, like `script.py --input_path /media/ex.png --output_path /media/out/` ? How to add them – zheyuanWang Dec 30 '22 at 11:17
10

Popen already generates a sub process to handle things. You just need to redirect the output pipes. Look at the subprocess documentation, specifically the section on popen stdin, stdout and stderr redirection.

If you don't redirect these pipes, it inherits them from the parent. Just be careful about deadlocking your processes.

You wanted additional windows for each subprocess. This is handled as well. Look at the startupinfo section of subprocess. It explains what options to set on windows to spawn a new terminal for each subprocess. Note that it requires the use of the shell=True option.

Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
Spencer Rathbun
  • 14,510
  • 6
  • 54
  • 73
2

This doesn't actually answer your question. But I've had my problems with subprocess too, and pexpect turned out to be really helpful.

Taku
  • 31,927
  • 11
  • 74
  • 85
Manuel Leuenberger
  • 2,327
  • 3
  • 21
  • 27
  • I doubt `pexpect` would be of much help because it requires the `pth` module because it isn't supported on Windows (which I believe the OP is using because he mentioned running ".exe" files). – martineau Jun 25 '11 at 02:18
  • for windows anyway [this answer](https://stackoverflow.com/a/20613015/7952027) would help. – Tejas Shetty Feb 04 '23 at 17:26