Execute terminal command from python in new terminal window?
There's no way to do this in general from a shell. What you have to do is run the terminal program itself, or some launcher program that does so for you. And the way to do that is different for each terminal program.
In some cases, os.startfile
will do what you want, but this isn't going to be universal.
Also, note in general, you're going to actually need an absolute path to your script, because the new terminal window will be running a new shell and therefore won't necessarily have your same working directory. But I'll ignore that for the examples.
With Windows cmd, the easiest way to do it is the start
shell command. If the thing you start
is any command-line program, including python
, it will get a new cmd window. So, something like:
subprocess.call('start /wait python bb.py', shell=True)
OS X has a similar command, open
. And it's a real program rather than a shell command, so you don't need shell=True
. However, running a command-line program or script with open
doesn't generally open a new terminal window. In fact, the whole point of it is to allow you to run programs as if they were being double-clicked in Finder, which never runs something in the terminal unless it's a .command file.
So, you can create a temporary .command
wrapper file and open
that; something like this (untested):
with tempfile.NamedTemporaryFile(suffix='.command') as f:
f.write('#!/bin/sh\npython bb.py\n')
subprocess.call(['open', '-W', f.name])
Alternatively, you can explicitly tell open
to use Terminal.app, something like this:
subprocess.call(['open', '-W', '-a', 'Terminal.app', 'python', '--args', 'bb.py'])
Or you can script Terminal.app via AppleEvents. For example:
appscript.app('Terminal').do_script('python bb.py')
The "do script" event opens a new window and runs its argument as a command. If you want more detailed control, open the scripting dictionary in AppleScript Editor and see all the fun stuff you can do.
On Linux or other *nix systems… well, there are 65,102 different desktop environments, launchers, and terminal programs. Do you need to work on all of them?
With gnome-terminal, just running the terminal again gives you a new window, and the -x
argument lets you specify an initial command, so:
subprocess.call(['gnome-terminal', '-x', 'python bb.py'])
Many older terminals try to be compatible with xterm
, which does the same thing with -e
, so:
subprocess.call(['xterm', '-e', 'python bb.py'])
subprocess.call(['rxvt', '-e', 'python bb.py'])
… etc.
How do you know which terminal the user is using? Good question. You could walk the like of parent processes from yourself until you find something that looks like a terminal. Or you could just assume everyone has xterm
. Or you could look at how various distros configure a default terminal and search for all of them. Or…
Open new gnome-terminal and run command
Like most terminals, gnome terminal needs options to execute commands:
gnome-terminal [-e, --command=STRING] [-x, --execute]
You probably need to add -x
option:
x, --execute
Execute the remainder of the command line inside the terminal.
so:
os.system("gnome-terminal -x python f.py")
That would not run your process in the background unless you add &
to your command line BTW.
The communicate
attempt would need a newline for your input but should work too, but complex processes like terminals don't "like" being redirected. It seems like using an interactive tool backwards.
And again, that would block until termination. What could work would be to use p.stdin.write("python f.py\n")
to give control to the python script. But in that case it's unlikely to work.
So it seems that you don't even need python do to what you want. You just need to run
python f.py &
in a shell.
Open a terminal in Python script and execute terminal commands in that newly opened terminal
I don't fully understand the wording of your question, but if you want to pop up a Terminal and run:
ls -l
in that Terminal, you can do:
import os
os.system("""osascript -e 'tell application "Terminal" to do script "ls -l"'""")
How to open a subprocess (within a python script) in new terminal and kill it after 15 seconds (Platform: Ubuntu)
Try this :
import subprocess
import time
proc = subprocess.Popen(('gnome-terminal', '--', 'file_name.sh'))
time.sleep(15)
subprocess.Popen(('pkill', '-f', 'file_name.sh'))
Python Script execute commands in Terminal
There are several ways to do this:
A simple way is using the os module:
import os
os.system("ls -l")
More complex things can be achieved with the subprocess module:
for example:
import subprocess
test = subprocess.Popen(["ping","-W","2","-c", "1", "192.168.1.70"], stdout=subprocess.PIPE)
output = test.communicate()[0]
Related Topics
Transparent Background in a Tkinter Window
How to Replace Two Things at Once in a String
Doing Something Before Program Exit
When and How to Use the Builtin Function Property() in Python
Counting Repeated Characters in a String in Python
What Is the Most Pythonic Way to Pop a Random Element from a List
Pandas Groupby and Select Rows with the Minimum Value in a Specific Column
Sorting Text File by Using Python
How to Send Cookies in a Post Request with the Python Requests Library
Python "Syntaxerror: Non-Ascii Character '\Xe2' in File"
What Happens When a Module Is Imported Twice
Print a String as Hexadecimal Bytes
Is There a Matplotlib Equivalent of Matlab's Datacursormode
Update a Dataframe in Pandas While Iterating Row by Row
Python Regex Match or Operator
Django Upgrading to 1.9 Error "Appregistrynotready: Apps Aren't Loaded Yet."