0

I'm executing a python script from php with way,

$mystring = system('python test.py', $retval);

and it works, I just would like to see error messages from the python script. Is it possible? It is returning output, for example, I see a "print "hello"" from the python script, but I dont see errors from the python scripts, it's obviously failing to load a module because execution stops there and I dont see that error displayed.

5
  • 2
    Possible duplicate of Php system() / exec() don't return output and there are others. In essence, try: $mystring = system('python test.py 2>&1', $retval);instead. This mixes stderr and stdout. Commented Jun 16, 2016 at 12:57
  • Actually, it returns output, for example, if the python script has a print I see it, but I can't see errors. I see "prints" before an import error but not after, and the import error message isnt displayed Commented Jun 16, 2016 at 13:01
  • 1
    Yes, and that is where the 2>&1 comes into play, it tells the system to write all that is intended for file descriptor 2 (the standard error stream out) instead to fd 1 (which is standard output if no one fiddles with it ;-) ). Please try the comment/answer and if it does what you want, enjoy and report back here. Thanks. Commented Jun 16, 2016 at 13:04
  • I just tried it.. with the last option I dont see output at all :) Commented Jun 16, 2016 at 13:08
  • What underlying shell on that hosting platform? Can you for testing log in via ssh and try in that folder directly a python test.py 2>&1 versus a python test.py 2> /dev/null and maybe show the python code here in the question? Commented Jun 16, 2016 at 13:15

1 Answer 1

0

It's better if you use in PHP:

exec('python test.py', $outp, $return);

You will have at $return the error status returned by the python script (there is an error if is not 0), and the error text at the $outp

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

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.