2

I am new python (ver 2.7) programmer and I have a question how to open 2 terminals and output different message.

The main task of this program is open 2 terminals and output different message. Right now, I have 3 following files

  1. test.py -- it is the main file that suppose to open 2 terminals and call different python 2 files
  2. print1.py -- this is simple 1 line of code which print the line of "this is 1st terminal"
  3. print2.py -- same as print1.py. it output "this is 2nd terminal"

Currently, my test.py has following code:

import subprocess
subprocess.call(['gnome-terminal','-x','python print1.py'])
subprocess.call(['gnome-terminal','-x','python print2.py'])

When I execute the program, it opens two terminals and both of them tell "There was an error creating the child process for this terminal. Failed to execute child" . Then, I have tried to write full path of print1/print2.py but it still gives same error and I am get stuck in there. Please some give me some advice to solve this error.

falsetru
  • 357,413
  • 63
  • 732
  • 636
Shin
  • 23
  • 1
  • 4
  • related: [How can I open two consoles from a single script](http://stackoverflow.com/q/19479504/4279). Have you tried the code from [my answer](http://stackoverflow.com/a/19797600/4279)? How exactly does it fail? – jfs Sep 13 '14 at 20:21

1 Answers1

1

You need to specify python and the file path as separated items. Otherwise, python print1.py is interpreted as a program name instead of python.

import subprocess
proc1 = subprocess.Popen(['gnome-terminal', '-x', 'python', 'print1.py'])
proc2 = subprocess.Popen(['gnome-terminal', '-x', 'python', 'print2.py'])
proc1.wait()
proc2.wait()

NOTE: I changed call with Popen. call waits the program terminate; the second terminal will not be executed until the first program terminate.

UPDATE

BTW, unless the scripts does not pause after print, gnome-terminal will close as soon as the python program terminate.

If you want shell prompt after the program termination, put following lines at the end of each file (print1.py, print2.py):

import os
os.execv('/bin/sh', ['sh'])
falsetru
  • 357,413
  • 63
  • 732
  • 636
  • Thank you for answering my question. From your advice, there is no error coming out now. However, your os.execv seems not working... Then, I have written this code in both print file for testing: print "this is first terminal" while 1: var = raw_input("Please enter something:) print var When I execute the program, it first open terminal then it close the terminal immediately. Could you give me some advice for this issue? – Shin Sep 13 '14 at 18:07
  • `os.execv('/bin/sh', ['sh'])` just works for me. Please run the `python print1.py` separately, then show me the error traceback if you have any. – falsetru Sep 13 '14 at 18:14
  • python print1.py works perfectly. it print out message then it start shell. Thus I do not believe that print1.py has the problem. But when I try to execute from test.py, it launches new terminal then close it immediately. I try to use both Popen and call but it seems it has same issue. – Shin Sep 13 '14 at 18:30
  • @Shin, What happen if you put `input()` at the end of the script? – falsetru Sep 13 '14 at 18:34
  • It still has same issue. From terminal, typing gnome-terminal -x python print1.py works perfectly fine. But when i use subprocess.call / Popen, it has the issue – Shin Sep 13 '14 at 18:44
  • @Shin, I changed the code in the answer to wait sub-processes. Please try it as is, and let me know the result. – falsetru Sep 13 '14 at 18:46
  • I have no idea why mine doesn't work.... I have pretty much same code as you have. – Shin Sep 13 '14 at 19:13
  • @Shin, Try this: `proc1 = subprocess.Popen(['gnome-terminal', '-x', 'sleep', '10'])`. Does the terminally close immediately? – falsetru Sep 13 '14 at 19:18
  • @Shin, Hmm.. it's strange. Are you sure you specified `subprocess.Popen(['gnome-terminal', '-x', 'python', 'print1.py'])` ? (separated items) – falsetru Sep 13 '14 at 19:27
  • yes, i specified it perfectly. Then I tried to change one of my print.py file to print 1111 import time time.wait(1000) First it displays 1111 then wait until given time – Shin Sep 13 '14 at 19:48
  • @Shin, Seems like `os.execv` does not work for your system. Try replace it with `subprocess`: `import subprocess; subprocess.call(['/bin/sh'])` – falsetru Sep 13 '14 at 19:51
  • Oh.... I finally got the right output!! At the end of your print file, I put import time time.sleep() Then it works perfectly. It opens up terminal and stay the shell up. Thank you so much of your advice! – Shin Sep 13 '14 at 19:54
  • @Shin, I'm curious why `os.execv` does not work for you. `subprocess` module use `os.exec*` internally. Anyway, it's glad to help you. :) – falsetru Sep 13 '14 at 19:55
  • No, os.execv works perfectly fine. what i need to do was add import time and time.sleep() at the end of you code. – Shin Sep 13 '14 at 19:58
  • @Shin, Do you mean, you appended `time.sleep(..)` after `os.execv(..)` call? – falsetru Sep 13 '14 at 20:00
  • Yes, I have appended time.sleep() after os.execv() then it worked. – Shin Sep 13 '14 at 22:17
  • So time.sleep was the real key to stay terminal open in my enviroment. I'm using Ubuntu 14.04 LTE 64-bit version and running on VirtualBox. if you are curious on my OS and environment, this is what I am using. – Shin Sep 13 '14 at 22:24
  • @Shin, I also using Ubuntu 14.04 64-bit (in real machine). – falsetru Sep 14 '14 at 03:44