Run program in new shell window from python

I am trying to run a python script from another python script in a new shell window. I can’t do it yet. Does anyone know how I can do this?

eg

import subprocess process = subprocess.Popen('test.py', shell=True, stdout=subprocess.PIPE) process.wait() print (process.returncode) 

when I run this script, it should run "test.py" in a new shell window.

I use linux, but it will be very useful if you can also provide a solution for windows.

+4
source share
2 answers

Here's how you could do it on Debian-like systems:

 import subprocess import shlex process = subprocess.Popen( shlex.split("""x-terminal-emulator -e 'bash -c "test.py"'"""), stdout=subprocess.PIPE) process.wait() print (process.returncode) 

Something like this should work for any * nix system.

Many thanks to eudoxos for pointing out x-terminal-emulator !

+3
source

Instead of starting the shell, launch the terminal where your script is running. On Linux, xterm -e test.py ; the Windows equivalent would be cmd.exe test.py I believe (but I could be wrong).

+1
source

All Articles