0

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.

Stefano Sanfilippo
33.2k7 gold badges85 silver badges83 bronze badges
asked Sep 23, 2013 at 7:44
2
  • 2
    Is TempHello.py executable? Don't you need a hashbang as well? Commented Sep 23, 2013 at 7:49
  • Thanks a lot @JoachimPileborg, silly mistake from my side. Thanks a lot. Also calling subprocess.Popen('python ./TempHello.py') works equally for me. Commented Sep 23, 2013 at 7:53

2 Answers 2

3

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.

answered Sep 23, 2013 at 7:52
Sign up to request clarification or add additional context in comments.

Comments

2

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

answered Sep 23, 2013 at 7:53

2 Comments

This solution requires a unix-like platform, it will not work on Windows and the like. Apparently, the OP is using Linux, but still he/she cannot consider this solution as being cross platform.
Thanks for your kind comments. But I guess the crux of the problem is pointed out and the question is solved in the simple answer.

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.