2

I am trying to call a simple python script

#!/usr/local/python25/bin/python
print "hello world"

from the following php script

 <?php
    echo exec("/usr/local/python25/bin/python myfile.py");
    ?>

But nothing was happened. Please tell me what is wrong here? (I also checked other thread but I could not solve my problem)

Question Solved: I forgot to give the permission to access /usr/local/python25/bin/python. After I did this, the problem solved. Thank you so much for your help!

2
  • are you sure the file is ran? maby try making a log file from the python script, also the myfile.py will be searched for on the current path your php script runs, are you sure the file is on that path. and lastly, you schipt should not return an error value to the shell executing it, php will ignore the results considering that the statement failed (try returning 1 in the python file) Commented Oct 14, 2011 at 11:15
  • Add $output and $status <?php echo exec("script path",$output,$status); ?> $output will contain whatever is produced by the python script $status will contain any error codes ( 0 means no errors ) tldp.org/LDP/abs/html/exitcodes.html Commented Nov 24, 2015 at 20:49

3 Answers 3

3

1.The exec function just return the last line from the result of the command.
2. The print statement in python (except python 3) automatically adds a newline at the end.

This is the reason you feel nothing was happened.

You can catch the whole output by this way.

exec("/usr/local/python25/bin/python myfile.py 2>&1", $output);
print_r($output);
Sign up to request clarification or add additional context in comments.

1 Comment

What I got on my browser was exactly like this -> "Array()".
1

Kind of an obvious point here, but can you run the python script from a terminal? Does it actually run?

Make sure the script is executable by whatever user PHP is running as - chmod 777 myfile.py, and just to be safe chmod 777 /usr/local/python25/bin/python. Also, make sure the python script is in the same directory as the PHP script, which is what your method of calling it requires.

Try changing your PHP script to this, and tell me what you see: (EDITED)

<?php

  // Path to the python script - either FULL path or relative to PHP script
  $pythonScript = 'myfile.py';

  // Path to python executable  - either FULL path or relative to PHP script
  $pythonExec = '/usr/local/python25/bin/python';

  // Check the file exists and PHP has permission to execute it
  clearstatcache();
  if (!file_exists($pythonExec)) {
    exit("The python executable '$pythonExec' does not exist!");
  }
  if (!is_executable($pythonExec)) {
    exit(("The python executable '$pythonExec' is not executable!"));
  }
  if (!file_exists($pythonScript)) {
    exit("The python script file '$pythonScript' does not exist!");
  }

  // Execute it, and redirect STDERR to STDOUT so we can see error messages as well
  exec("$pythonExec \"$pythonScript\" 2>&1", $output);

  // Show the output of the script
  print_r($output);

?>

2 Comments

Thanks for answering my question. But again nothing showed up on my browser. That meant no errors but the the command exec was not executed.
So what do you get? Blank page, or Array ()? Also, try adding the lines ini_set('display_errors',1); error_reporting(-1); to the top of the script...
0

If you want to capture the subprocess' stdout, you should use passthru

Also you don't need the first line of that python script if you're calling the python interpreter directly.

5 Comments

Thank you for answering but I just simply want to see "hello world" on my browser. I think it should work but ...
Yes. The print sends the output to the process' STDOUT stream. That will probably end up being the STDOUT of the PHP process which may be in an Apache log. If you want send that stream and send it to the browser, you will need to capture it. You should understand that the python print and the php echo are very very different. PHP sends 'echo' output to the HTTP stream. Python sends it to STDOUT.
@Joe So what about when you use echo in cli?
@Joe: Sorry but I tried to change from exec(...) to passthru(...) but still did not work.
@xdazz - It's been a while since I wrote PHP but consider this: When you run PHP from the command line, you're running a single process, and echo does go to stdout. But running on a server, the output has to go to the appropriate HTTP request/response. I can't be bothered to look into it, but I imagine that the PHP process is probably threaded, serving several concurrent requests per process. They can't all share the same STDOUT! So echo/STDOUT is probably a special case for CLI.

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.