4
from subprocess import call
call(["python3", "/home/johngr/psdirc/TestBot1.py"]) and call(["python3", "/home/johngr/psdirc/TestBot2.py"]) and call(["python3", "/home/johngr/psdirc/TestBot3.py"])

The call is working but it only runs the first file. I want them all to run in their own terminal windows.

handsomebob10
  • 49
  • 1
  • 6
  • This question is unclear. Do you want to run them serially? At a time? What do you mean "in a own cmd"? – Charles Duffy May 15 '15 at 18:37
  • 1
    "in multiple cmds" -- `cmd.exe` is a shell; it has nothing to do with terminal windows. This question would be clearer if you said "in multiple windows", though you'd need to also specify your operating system, since any kind of dealing with windows is OS-specific. – Charles Duffy May 15 '15 at 18:38
  • I need them to run in multiple windows. – handsomebob10 May 15 '15 at 18:48
  • @handsomebob10, any reason why you are changing your code? This most recent change invalidates one of the answers. – gunr2171 May 15 '15 at 19:14
  • gunr2171 I tried CivFan's suggestion it gave same results as Padraic Cunningham's answer I just removed the ands. – handsomebob10 May 15 '15 at 19:18
  • 1
    @handsomebob10, don't change the code in your question, the only thing you needed to do was add that you want each script run in it's own terminal window, that is the most important part of the question. – Padraic Cunningham May 15 '15 at 19:39
  • Padraic Cunningham oh ok sorry for that. – handsomebob10 May 15 '15 at 19:40
  • @handsomebob10, have you run the code I added? – Padraic Cunningham May 15 '15 at 19:45
  • I ran it and got: Traceback (most recent call last): File "untitled.py", line 26, in call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot1.py"]) File "/usr/lib/python2.7/subprocess.py", line 522, in call return Popen(*popenargs, **kwargs).wait() File "/usr/lib/python2.7/subprocess.py", line 710, in __init__ errread, errwrite) File "/usr/lib/python2.7/subprocess.py", line 1327, in _execute_child raise child_exception OSError: [Errno 2] No such file or directory – handsomebob10 May 15 '15 at 19:49
  • what terminal do you have installed? – Padraic Cunningham May 15 '15 at 19:51
  • @handsomebob10, well, that means you don't have `gnome-terminal` installed. Depending on your operating system, this may or may not be expected. Have you actually told anyone what OS you're running? (I **did** say, an hour or so ago, that "you'd need to also specify your operating system"). – Charles Duffy May 15 '15 at 19:51
  • Oh I have LXTerminal my operative system is ubuntu. Sorry for not specifying. – handsomebob10 May 15 '15 at 19:55
  • related: [Execute terminal command from python in new terminal window?](http://stackoverflow.com/q/19308415/4279) – jfs May 16 '15 at 17:36
  • [keep multiple cmd windows open from batch](http://stackoverflow.com/a/12144179/4279) – jfs May 16 '15 at 17:37

2 Answers2

5

Don't use and just run one after the other:

call(["python3", "/home/johngr/psdirc/TestBot1.py"])
call(["python3", "/home/johngr/psdirc/TestBot2.py"])
call(["python3", "/home/johngr/psdirc/TestBot3.py"])

If you don't want them to wait for the process to finish before starting the next use Popen:

 Popen(["python3", "/home/johngr/psdirc/TestBot1.py"])
 Popen(["python3", "/home/johngr/psdirc/TestBot2.py"])
 Popen(["python3", "/home/johngr/psdirc/TestBot3.py"])

call will Run the command described by args. Wait for command to complete, then return the returncode attribute. where Popen won't wait.

If you want to be sure each process exits with a non-zero exit status use check_call which will raise a CalledProcessError for any non-zero exit status.

To open a terminal for each you can use gnome-terminal with -e Execute the argument to this option inside the terminal:

call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot1.py"])
call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot2.py"])
call(['gnome-terminal', '-e', "python3 /home/johngr/psdirc/TestBot3.py"])

If you want to open tabs you can use --tab -e:

cmd =['gnome-terminal', '--tab', '-e', 'python3 /home/johngr/psdirc/TestBot1.py',
      '--tab', '-e','python3 /home/johngr/psdirc/TestBot2.py','--tab', '-e', 
      'python 3 /home/johngr/psdirc/TestBot3.py']
call(cmd)

You don't seem to have gnome-terminal so just replace it with lxterminal:

call(['lxterminal', '-e', 'python3 /home/johngr/psdirc/TestBot1.py'])

Not sure if --tab option is supported or not, I don't see any reference to it in the documentation.

Padraic Cunningham
  • 176,452
  • 29
  • 245
  • 321
1

Answer to updated question

Use subprocess.Popen:

from subprocess import Popen, PIPE

bot1 = Popen(["lxterminal", "-e", "python3", "-i", "/home/johngr/psdirc/TestBot1.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
bot2 = Popen(["lxterminal", "-e", "python3", "-i", "/home/johngr/psdirc/TestBot2.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)
bot3 = Popen(["lxterminal", "-e", "python3", "-i", "/home/johngr/psdirc/TestBot3.py"], stdout=PIPE, stderr=PIPE, stdin=PIPE)

This will put each into its own window. The -i option for python3 is to make the window interactive after the TestBot3.py script finishes. It doesn't hurt to have, even if you don't expect it to be interactive, so you can debug in case something does go wrong.

I did a test, and the new windows persist after exiting this script.

Answer to original question

I don't recommend this for production code, but:

not call(["python3", "/home/johngr/psdirc/TestBot1.py"]) \
    and not call(["python3", "/home/johngr/psdirc/TestBot2.py"]) \
    and not call(["python3", "/home/johngr/psdirc/TestBot3.py"])

Python treats the bash "Good" return code of 0 as False, and the and operator is lazy.

This of course presumes you expect each call to succeed, otherwise you will still not call all three. So you're much better off calling each on a separate line. Wrap it in a function if you want to clean up the code.

CivFan
  • 13,560
  • 9
  • 41
  • 58
  • Oh nvm I got it working :D it's LXTerminal in lower case xD. lxterminal. Thank you for your help. The result is: call(["lxterminal", "-e", "python3", "/home/johngr/psdirc/TestBot2.py"]) call(["lxterminal", "-e", "python3", "/home/johngr/psdirc/TestBot3.py"]) – handsomebob10 May 15 '15 at 22:11