-1

Im trying to run more than one different .exe file in console.

i used this code to run it and it works fine.

import os
import subprocess

print('1. App1')
print('2. App2')
print('3. App3')
print('4. App4')
print('5. Exit')

while(True):
    run = input('Choose App : ')
    if run == '1':
        subprocess.call('App1.exe', creationflags=subprocess.CREATE_NEW_CONSOLE)
    elif run == '2':
        subprocess.call('App2.exe', creationflags=subprocess.CREATE_NEW_CONSOLE)
    elif run == '3':
        subprocess.call('App3.exe', creationflags=subprocess.CREATE_NEW_CONSOLE)
    elif run == '4':
        subprocess.call('App4.exe', creationflags=subprocess.CREATE_NEW_CONSOLE)
    else
        exit()

But the input loop only works when the first app that opened close. So i can input another app to run.

The think is i need to input many app to run in the same time without waiting the first app closed. I tried to add some return fuction but it give me an error message

SyntaxError: 'return' outside function

anyone know how to make it work for opening many app at the same time using subprocess.call without waiting the first app closed ?


so i tried with Popen but when i choose the input apps it give me an error

File ".exe", line 1 SyntaxError: Non-UTF-8 code starting with '\x90' in file .exe on line 1, but no encoding declared; see python.org/dev/peps/pep-0263 for details

This is the code :

import sys
import os
from subprocess import Popen, PIPE

print('1. App1')
print('2. App2')

while(True):
    run = input('Choose App : ')
    if run == '1':
        run = Popen([sys.executable, "App1.exe"])
        run.communicate()
    elif run == '2':
        run = Popen([sys.executable, "App2.exe"])
        run.communicate()
    else:
        exit()
rullymnh
  • 55
  • 6
  • 2
    Why did you add the `sys.executable`? Try just `Popen("App1.exe")` – Tomerikoo Oct 31 '20 at 16:47
  • because i want the `App1.exe` opened in new window console, without sys.executable it just open the `App1.exe` in the same window console and i can't open the `App2.exe` at the sametime, or i mistaken the `sys.executable` function ? – rullymnh Oct 31 '20 at 16:58
  • Try adding `shell=True` – Tomerikoo Oct 31 '20 at 17:04
  • i tried adding `shell=True` with `sys.executable` it give same error message, and i tried without `sys.executable` app opened in same window console. – rullymnh Oct 31 '20 at 17:09
  • Try the `creationflags=subprocess.CREATE_NEW_CONSOLE` as you did with the `call` (but without `sys.executable`) – Tomerikoo Oct 31 '20 at 17:11
  • 2
    Does this answer your question? [subprocess.Popen in different console](https://stackoverflow.com/questions/15899798/subprocess-popen-in-different-console) – Tomerikoo Oct 31 '20 at 17:12
  • adding `creationflags=subprocess.CREATE_NEW_CONSOLE` in Popen give me an error message `NameError: name 'subprocess' is not defined` – rullymnh Oct 31 '20 at 17:15
  • Okay because you did `from subprocess import ...` dude I can't spoon-feed you... add `CREATE_NEW_CONSOLE` to the `from subprocess import ...` line... – Tomerikoo Oct 31 '20 at 17:31
  • i already did that but still – rullymnh Oct 31 '20 at 17:38
  • nvm, it works with `creationflags=CREATE_NEW_CONSOLE` without `subprocess.`. Thanks for the help @Tomerikoo – rullymnh Oct 31 '20 at 17:42
  • 1
    Of course, if you do `from subprocess import x` you don't need to do `subproccess.x` you just use `x`... – Tomerikoo Oct 31 '20 at 18:46
  • ahh i see, so thats my problem the whole time. Thanks for telling me this. – rullymnh Oct 31 '20 at 21:27

1 Answers1

2

Both subprocess.call and subprocess.run wait for the process to be complete before returning. One thing you could do is use Popen instead, which starts the process in a new process which you can communicate with.

Take a look at: https://docs.python.org/3.8/library/subprocess.html#popen-objects

iHowell
  • 2,263
  • 1
  • 25
  • 49
  • so thats not possible for subprocess to make it work ? – rullymnh Oct 30 '20 at 21:08
  • `Popen` is part of the subprocess module. – iHowell Oct 30 '20 at 21:09
  • so i just tried it with Popen but when i choose the input apps it give me an error " File ".exe", line 1 SyntaxError: Non-UTF-8 code starting with '\x90' in file .exe on line 1, but no encoding declared; see http://python.org/dev/peps/pep-0263/ for details" do you know how to fix it ? – rullymnh Oct 30 '20 at 21:49
  • @rullymnh Can you edit your original question with the Popen changes you made? – wholevinski Oct 31 '20 at 16:29