0

I know there are tons of questions on this topic but none seem to provide an answer that solves all my requirements.

My requirements:

  • Start another application (should support all kinds of applications: long running processes taking user input, simple applications ending quickly or applications that take a long time)
  • Should also be able to start applications that end immediately (ex: 'git status')
  • It needs to start in a new console window
  • The console window should remain active even after command completion to see the application output.
  • It needs to be cross platform (Windows and Linux)

I want to use subprocess module. I already tried using the option CREATE_NEW_CONSOLE

subprocess.Popen(("git", "status"), close_fds=True, creationflags=subprocess.CREATE_NEW_CONSOLE | subprocess.STARTF_USESHOWWINDOW, shell=True)

but:

  • It does not really work. I see no window starting, or may be it is starting but exiting too fast after command execution.
  • this only works for Windows. Was wondering if there is a way to do this on all platforms consistently ?
martineau
  • 119,623
  • 25
  • 170
  • 301
lightsunray
  • 409
  • 5
  • 16
  • some programs run system script (bash/bat) which runs expected program and `pause` program so window waits with results till you press some key. – furas Dec 03 '19 at 08:18

1 Answers1

0

One approach would be to open a new console window with subprocess and run the command in that window. On Linux that can be achieved with the "-e" flag followed by the command.

For example (based on https://stackoverflow.com/a/3531426)

x-terminal-emulator -e "bash -c \"git status; exec bash\""

will open a new window of the default terminal and run the command

bash -c "git status; exec bash"

in that window. In Python this would look like

subprocess.Popen('x-terminal-emulator -e "bash -c \\"git status; exec bash\\""', shell=True)

Running "exec bash" at the end opens a new shell in the console window and prevents it from exiting immediately.


According to this answer, on Windows you can use "start cmd /k" and then run the command. I haven't tried it, but presumably something like

subprocess.Popen('start cmd /k git status', shell=True)

will do the trick.

dom1310df
  • 7
  • 2
  • 9
  • Although it is a working solution, one big disadvantage especially on Linux is one needs to know which terminal is installed before making a call. For example, I have 'konsole', but never know what others have !!! – lightsunray Dec 03 '19 at 13:23
  • The command "x-terminal-emulator" should on most systems open the default terminal emulator, and should still work with the "-e" option. – dom1310df Dec 06 '19 at 19:28