Reputation: 20856
I have 2 modules test1.py & test2.py and both are located under c:/python27, From test1.py, Im trying to call test.py as shown below but I get an error.
Test1.py
import subprocess
print 'Im in module-1'
subprocess.Popen('c:/python27/test2.py')
test2.py
print 'Im in module-2'
Error:-
C:\Python27>python test1.py
Im in module-1
Traceback (most recent call last):
File "test1.py", line 4, in <module>
subprocess.Popen('c:/python27/test2.py')
File "C:\Python27\lib\subprocess.py", line 679, in __init__
errread, errwrite)
File "C:\Python27\lib\subprocess.py", line 896, in _execute_child
startupinfo)
WindowsError: [Error 193] %1 is not a valid Win32 application
Upvotes: 0
Views: 1965
Reputation: 250951
If the environment variable is set then try this:
import subprocess
print 'Im in module-1'
subprocess.Popen(['python','c:/python27/test2.py'])
If the environment variable is not set then use sys.executable
:
import sys
subprocess.Popen([sys.executable,'c:/python27/test2.py'])
If you want to check the output then use subprocess.check_output
:
print subprocess.check_output(['python','c:/python27/test2.py'])
Upvotes: 2
Reputation: 7309
I think the problem here is that subprocess
fires up a brand new subshell, one that can differ significantly shell you are used to working in. Not sure how it all works for windows, but for instance in unix, subprocess will use /bin/sh
by default, which is a simpler shell with less features. I'm guessing the default shell subprocess
is using on your windows machine doesn't know what to do with the .py
. Best to specify an interpreter explicitly as others have mentioned.
This may be a good solution for you: https://stackoverflow.com/a/912847/1583239
Upvotes: 1
Reputation: 1160
Notice that python scripts must be run using the python command.
c:>python 'c:/python27/test2.py'
Window's doesn't know how to run .py files.
Upvotes: -2