34

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,030
  • 21
  • 100
  • 124
emiguel
  • 383
  • 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
  • @Eder You should accept Spencer's answer. – Jim Clay Jun 24 '11 at 16:52
  • @Jim: thanks! This is my second time and I didn't know. – emiguel Jun 26 '11 at 08:31
  • @Eder No problem. I'm glad you found a solution to your problem. – Jim Clay Jun 28 '11 at 23:11

3 Answers3

49

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,030
  • 21
  • 100
  • 124
Pedro Vagner
  • 8,596
  • 3
  • 27
  • 20
  • 9
    Python 3 uses input instead of raw_input. – Brōtsyorfuzthrāx May 04 '14 at 00:44
  • 4
    aaaaaahhhhhhhhh!!!! took me 30 minutes just now to find this particular answer. Thanks and may you get another 10K upvotes for it. – velis Nov 07 '14 at 06:21
  • 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
  • 4
    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
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,030
  • 21
  • 100
  • 124
Spencer Rathbun
  • 13,966
  • 5
  • 51
  • 72
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
  • 28,570
  • 11
  • 65
  • 77
Manuel Leuenberger
  • 2,297
  • 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