1

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?

ejj28
  • 88
  • 2
  • 12
  • Likely dupe: http://stackoverflow.com/questions/989349/running-a-command-in-a-new-mac-os-x-terminal-window [see osascript solution] – Łukasz Rogalski Oct 03 '16 at 22:57

2 Answers2

0

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

subprocess.call(['/usr/bin/python', 'test.py'])
Brendan Abel
  • 35,343
  • 14
  • 88
  • 118
  • 1
    Just to clarify, I need it in a terminal window so that the user of my script can see the output and interact with the script. Thanks for answering though! – ejj28 Oct 03 '16 at 21:36
0

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.

Al Sweigart
  • 11,566
  • 10
  • 64
  • 92