8

I'm trying to write a script that opens a new terminal then runs a separate python script from that terminal.

I've tried:

os.system("gnome-terminal 'python f.py'")

and

p = Popen("/usr/bin/gnome-terminal", stdin=PIPE)
p.communicate("python f.py")

but both methods only open a new terminal and do not run f.py. How would I go about opening the terminal AND running a separate script?

Edit: I would like to open a new terminal window because f.py is a simply server that is running serve_forever(). I'd like the original terminal window to stay "free" to run other commands.

Joe
  • 587
  • 1
  • 8
  • 15

5 Answers5

5

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.

Community
  • 1
  • 1
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
2

As of GNOME Terminal 3.24.2 Using VTE version 0.48.4 +GNUTLS -PCRE2

Option “-x” is deprecated and might be removed in a later version of gnome-terminal. Use “-- ” to terminate the options and put the command line to execute after it.

Thus the preferred syntax appears to be

gnome-terminal -- echo hello

rather than

gnome-terminal -x echo hello
dardisco
  • 5,086
  • 2
  • 39
  • 54
1

Here is a complete example of how you would call a executable python file with subprocess.call Using argparse to properly parse the input.

the target process will print your given input.

Your python file to be called:

import argparse    
parser = argparse.ArgumentParser()

parser.add_argument("--file", help="Just A test", dest='myfile')
args = parser.parse_args()
print args.myfile

Your calling python file:

from subprocess import call


#call(["python","/users/dev/python/sandboxArgParse.py", "--file", "abcd.txt"])
call(["gnome-terminal", "-e", "python /users/dev/python/sandboxArgParse.py --file abcd.txt"])

Just for information: You probably don't need python calling another python script to run a terminal window with a process, but could do as follows:

gnome-terminal -e "python /yourfile.py -f yourTestfile.txt"
user1767754
  • 23,311
  • 18
  • 141
  • 164
  • Thanks for the detailed response. I clarified my question as the python script I'm calling is a simple server utilizing `serve_forever()`. Therefore, I'd like to run the script in a new terminal window so that my current terminal isn't tied up. – Joe Nov 27 '17 at 20:17
  • That's easy just use call with `gnome-terminal` – user1767754 Nov 27 '17 at 20:25
1

The following code will open a new terminal and execute the process:

process = subprocess.Popen(
    "sudo gnome-terminal -x python f.py", 
    stdout=subprocess.PIPE,
    stderr=None,
    shell=True
)
Omar Einea
  • 2,478
  • 7
  • 23
  • 35
M.Girish Babu
  • 39
  • 1
  • 8
0

I am running a uWS server with this.In my case Popen didn't help(Even though it run the executable, still it couldn't communicate with a client -: socket connection is broken).This is working.Also now they recommends to use "--" instead of "-e".

subprocess.call(['gnome-terminal', "--", "python3", "server_deployment.py"])

#server_deployment.py

def run():
    execution_cmd = "./my_executable arg1 arg2 dll_1 dll_2"
    os.system(execution_cmd)
run()