Launch Python script in new terminal

1.3k Views Asked by At

I want to launch a python script in a new macOS terminal window from another script. I'm currently using this code:

subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python'])

which launches a python prompt in a new terminal window. But when I try to run a python script with this code:

subprocess.call(['open', '-a', 'Terminal.app', '/usr/bin/python', 'test.py'])

it completely ignores the 'test.py' on the end and starts a python prompt, just like it does without the test.py on the end.

How can I make this work?

2

There are 2 best solutions below

0
On

This Python code will open a new terminal window, and then have python3 run test.py:

import os
os.system("""osascript -e 'tell application "Terminal" to do script "python3 test.py"'""")

I'm unable to come up with a way to get this into a subprocess.call() call.

1
On

Don't use the open -a terminal.app, just use the python executable

subprocess.call(['/usr/bin/python', 'test.py'])