Reputation: 1167
I had written following 2 programs,
# TempHello.py
def sayHello():
print 'Hello World !'
sayHello()
# Subprocess.py
import subprocess
if __name__ == '__main__':
print 'Calling other program'
child = subprocess.Popen( "./TempHello.py" , shell=True)
print subprocess.check_output()
print 'Calling other program completed'
When I try to run the Subprocess.py program, it gives following error as,
Calling other program
./TempHello.py: 2: ./TempHello.py: Syntax error: "(" unexpected
Traceback (most recent call last):
File "/usr/mandar/AnuntaTech/eclipse_workspace/BackupManager/Subprocess.py", line 7, in <module>
print subprocess.check_output()
File "/usr/lib/python2.7/subprocess.py", line 537, in check_output
process = Popen(stdout=PIPE, *popenargs, **kwargs)
TypeError: __init__() takes at least 2 arguments (2 given)
I am not able to figure out how to solve this problem. Please help.
Upvotes: 0
Views: 934
Reputation: 4532
You are running TempHello.py as a program. But it is just python source code. How about putting this line in the beginning:
#!/usr/bin/env python
and run
chmod u+x TempHello.py
before running Subprocess.py
Upvotes: 2
Reputation: 33046
Problem is that Popen
is trying to execute TempHello.py
as if it were a shell executable, when in fact it is a Python script. Simplest solution is fixing Subprocess.py
like this:
import subprocess
if __name__ == '__main__':
print 'Calling other program'
child = subprocess.Popen( "python TempHello.py" , shell=True)
print subprocess.check_output()
print 'Calling other program completed'
In fact, you'll want to invoke Python executable and make it run your script.
On *nix platforms (thus, not including Windows), you can also use a shebang to specify which interpreter to use, like this:
#! /usr/bin/env python
def sayHello():
print 'Hello World !'
sayHello()
Or
#! /usr/bin/python
def sayHello():
print 'Hello World !'
sayHello()
and make the script executable with chmod u+x TempHello.py
.
By the way, I suggest you to take a different approach in spawning Python scripts, if that's your purpose: have a look at multiprocess
module.
Upvotes: 3