41

I hope this is not a duplicate.

I'm trying to use subprocess.Popen() to open a script in a separate console. I've tried setting the shell=True parameter but that didn't do the trick.

I use a 32 bit Python 2.7 on a 64 bit Windows 7.

Ionut Hulub
  • 6,180
  • 5
  • 26
  • 55
  • You didn't show an actual code base, so i just made up a command example below based on `dir` in windows and `ls` in linux for listing folder contents. – Torxed Apr 09 '13 at 10:47
  • @Torxed I tried your code and there are a few things that I don't like: I don't see a new console poping up. I would like a second console to be created and to be able to read the output of the program that I run with `popen` in the new console. In my main program, I don't need the output of the program I run with `popen`. I just need to see it in a different console. Could you please provide an example of how this should be implemented? – Ionut Hulub Apr 09 '13 at 10:49
  • That wasn't clear from your original problem description, because you see, "open a script in a separate console" that's exactly what `.Popen()` does, and if you check your process monitor you'll notice that a cmd.exe is launched from you calling `.Popen()`, it's just not visible because it's a subprocess to your application and it's "muted". You don't typically open new GUI windows just to execute tasks, so may i ask why you want this? because it sounds backwards to me :/ – Torxed Apr 09 '13 at 11:00
  • Thank you... Also, how would I be able to make my main program wait until the second console finishes execution and then get the return code? – Ionut Hulub Apr 09 '13 at 11:04
  • IMO, Don't open a extra GUI. execute `X` with my first example, grab the exit code and print only valuable information if needed and ignore the rest of the output. It's faster and more elegant. Anyways, `os.system()` gives you the returncode of your command and the program will wait for the command to finish since you're not utelizing threads, see http://docs.python.org/2/library/threading.html for help with threads. – Torxed Apr 09 '13 at 11:07
  • I do get the exit code but the proble is that I see the output of the program I run with `os.system` in the same windows that calls the previously mentioned function. It does not appear in a seprate console. Do you know any workaround? – Ionut Hulub Apr 09 '13 at 11:15
  • See my edit using `start` on windows, also, try googling that.. it's the first hit for "python windows open new console" – Torxed Apr 09 '13 at 11:19
  • related: [How can I spawn new shells to run python scripts from a base python script?](http://stackoverflow.com/q/6469655/4279) – jfs Apr 10 '14 at 03:01

3 Answers3

60

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

from subprocess import Popen, CREATE_NEW_CONSOLE

Popen('cmd', creationflags=CREATE_NEW_CONSOLE)

input('Enter to exit from Python script...')

Related

How can I spawn new shells to run python scripts from a base python script?

Community
  • 1
  • 1
  • that's what I was looking for, thanks :) ... now I have to set a tempo before launching my other task ... – A.Joly Sep 12 '17 at 13:45
  • This should be the accepted answer. The solid way of opening a new console window in Windows is explicitly supplying the WINAPI process creation flags. Other solutions rely on Python implementation side-effects. – Andrey Moiseev Oct 12 '19 at 17:14
  • Read way too much comments before testing this solution. Thx! – Greg7000 Sep 19 '22 at 14:17
9
from subprocess import *

c = 'dir' #Windows

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE, shell=True)
print handle.stdout.read()
handle.flush()

If you don't use shell=True you'll have to supply Popen() with a list instead of a command string, example:

c = ['ls', '-l'] #Linux

and then open it without shell.

handle = Popen(c, stdin=PIPE, stderr=PIPE, stdout=PIPE)
print handle.stdout.read()
handle.flush()

This is the most manual and flexible way you can call a subprocess from Python. If you just want the output, go for:

from subproccess import check_output
print check_output('dir')

To open a new console GUI window and execute X:

import os
os.system("start cmd /K dir") #/K remains the window, /C executes and dies (popup)
Torxed
  • 22,866
  • 14
  • 82
  • 131
2

On Linux shell=True will do the trick:

command = 'python someFile.py' subprocess.Popen('xterm -hold -e "%s"' % command)

Doesn't work with gnome-terminal as described here:

https://bbs.archlinux.org/viewtopic.php?id=180103

blented
  • 2,699
  • 2
  • 23
  • 17