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.

1

1 Answer 1

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'])
Sign up to request clarification or add additional context in comments.

18 Comments

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?
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.
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, What happen if you put input() at the end of the script?
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
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.