0

I've seen on here a few other issues running subprocesses with Python, but none have solved the issue I'm having at the moment. Relatively new to Python, so just playing around and I'm sure it's a simple solution.. But I just can't make it work!

I want a subprocess to return some data, and it isn't. So I wrote this basic example to try and get it working but it still isn't. Where am I going wrong? I'm getting no errors or anything, it's just not doing anything.

sp_test.py

import os
import subprocess
def main():
 subp = subprocess.check_call(['python', 'sp.py'])
 print subp
if __name__ == '__main__':
 main()

and...

sp.py

def do_something():
 return "Hello World!"
do_something()
exit()
asked Oct 18, 2014 at 14:26
2
  • Check call returns to the returncode only. Also you will need to print Hello World. Since the only way is to capture the printed output. Use check_output maybe? Commented Oct 18, 2014 at 14:28
  • If you want to see the output use print(do_something()) Commented Oct 18, 2014 at 14:48

1 Answer 1

1

There are two issues with your implementation.

1.

check_call only returns the returncode of 0 or throws an exception otherwise. (See https://docs.python.org/2/library/subprocess.html#subprocess.call)

2.

Also you are trying to capture the the value of Hello World! by simply returning a value. That won't work using subprocess. If you want to do that, you may want to look into something like Pyro4 (https://pythonhosted.org/Pyro4/intro.html)


Here is one solution if you want to use subprocess still.

First you can use check_output (https://docs.python.org/2/library/subprocess.html#subprocess.check_output). This will capture any output from your subprocess.

Then in your sp.py program, you will need to print the result do_something.

sp_test.py

import os
import subprocess
def main():
 subp = subprocess.check_output(['python', 'sp.py'])
 print subp
if __name__ == '__main__':
 main()

sp.py

def do_something():
 return "Hello World!"
print do_something()
exit()
answered Oct 18, 2014 at 14:30
Sign up to request clarification or add additional context in comments.

8 Comments

Thanks for the info, I'll try and get it working with it! My current implementation prints out nothing. Shouldn't it at least print out the return code?
Yeah it should. I tested your script on my system and it worked. It printed 0
Interesting.. It prints nothing for me!
Shouldn't doesn't make a difference, but what OS are you using?
Maybe try this simple example: subprocess.check_call(['echo', 'Hello World!'])
|

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.