2

I'm adjusting my code I wrote on Linux for Win 7.

On Linux it was:

subprocess.call(['./myscript.py', arg1, arg2, arg3])

It was launched from Shell and everything was working fine. For windows (I'm using Python from Idle) I made it:

subprocess.call(['myscript.py', arg1, arg2, arg3],  shell=True)

It seems not to launch anything, but isn't giving me back any error. I tried to debug the function with pdb.set_trace() and the checkpoints inside myscript.py aren't showing up.

Martijn Pieters
  • 1,048,767
  • 296
  • 4,058
  • 3,343
user2018915
  • 175
  • 1
  • 1
  • 5
  • Why use `shell=True` there at all? – Martijn Pieters Mar 20 '13 at 11:48
  • Otherwise I was getting Error 193: %1 is not a valid Win32 application – user2018915 Mar 20 '13 at 11:50
  • I think in linux it was an executable, hence `./myscript.py`. In windows, it's most probably not. Use `python myscript.py` somehow. – Bibhas Debnath Mar 20 '13 at 11:51
  • If I make it as subprocess.Popen(['python myscript.py', 'arg1'], shell=True) it's also not launching anything without giving any error. WHAT IS MORE, if I give a wrong name of the file, it's also not recognizing any error. – user2018915 Mar 20 '13 at 11:56
  • 1
    Of course it wont launch anything, You have to put full path of `python`. Check the question @MartijnPieters posted. It will solve your problem. – Bibhas Debnath Mar 20 '13 at 12:03

1 Answers1

1

As in the comments and the linked question, in Windows you cannot simply execute the python script, you also need to provide the path to the python executable. As you're already using python, you can find the location of the executable simply with sys.executable

import sys

p = subprocess.Popen([sys.executable, 'myscript.py', arg1, arg2, arg3])

The output is being piped to stderr and stdout - to see it you'll also need communicate

(stdoutdata, stderrdata) = p.communicate()

print stdoutdata, 
print stderrdata
danodonovan
  • 19,636
  • 10
  • 70
  • 78