4

I am trying to launch a python script from another python script in a new shell window. So far I'm unable to do it. Does anyone knows how can I accomplish this?

for example

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

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

I'm using linux, but it will be very helpful if you can provide solution for windows too.

2 Answers 2

3

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 it should work for any *nix system.

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

Sign up to request clarification or add additional context in comments.

3 Comments

x-terminal-emulator is (at least on Debian and Ubutnu) symlink to the user-selected terminal, and should work always (unlike gnome-terminal)
x-terminal-emulator is set at the OS level, not on a per-user basis. I don't think it helps to replace gnome-terminal with x-terminal-emulator since, if x-terminal-emulator points to something other than gnome-terminal then the arguments -x bash -c "test.py" are likely to be invalid or nonsensical.
Yes, sorry, I realized that debian alternatives were system-wide. However, most terminal emulators mimick options of the classical xterm program, so using -e as the last option (with quoted command) should work just fine for any terminal emulator.
1

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.