0

I try to run simple script in windows in the same shell. When I run

subprocess.call(["python.exe", "a.py"], shell=False)

It works fine.

But when I run

subprocess.Popen(["python.exe", "a.py"], shell=False)

It opens new shell and the shell=false has no affect.

a.py just print message to the screen.

Hila
  • 189
  • 1
  • 13
  • Possible duplicate of [Python: subprocess call with shell=False not working](http://stackoverflow.com/questions/25465700/python-subprocess-call-with-shell-false-not-working) – Chanda Korat Feb 22 '17 at 12:32

1 Answers1

0

First calling Popen with shell=False doesn't mean that the underlying python won't try to open a window/console. It's just that the current python instance executes python.exe directly and not in a system shell (cmd or sh).

Second, Popen returns a handle on the process, and you have to perform a wait() on this handle for it to end properly or you could generate a defunct process (depending on the platform you're running on). I suggest that you try

p = subprocess.Popen(["python.exe", "a.py"], shell=False)
return_code = p.wait()

to wait for process termination and get return code.

Note that Popen is a very bad way to run processes in background. The best way would be to use a separate thread

import subprocess
import threading

def run_it():
    subprocess.call(["python.exe", "a.py"], shell=False)

t = threading.Thread(target=run_it)
t.start()
# do your stuff

# in the end
t.join()
Jean-François Fabre
  • 137,073
  • 23
  • 153
  • 219
  • But I don't want to wait until the process will finish.. this is the reason I use Popen and not call. – Hila Feb 22 '17 at 12:35
  • Thanks, the problem is that it still waits until the thread finish... I want that my code will run, execute another process and exit. I don't want to wait for the process to finish. – Hila Feb 22 '17 at 16:55