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!

phucle
  • 31
  • 1
  • 4
  • 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) – Paul Scheltema Oct 14 '11 at 11:15
  • Add $output and $status `` $output will contain whatever is produced by the python script $status will contain any error codes ( 0 means no errors ) http://www.tldp.org/LDP/abs/html/exitcodes.html – b_dubb Nov 24 '15 at 20:49

3 Answers3

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);
xdazz
  • 158,678
  • 38
  • 247
  • 274
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);

?>
DaveRandom
  • 87,921
  • 11
  • 154
  • 174
  • 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. – phucle Oct 14 '11 at 10:09
  • 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... – DaveRandom Oct 14 '11 at 10:10
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.

Joe
  • 46,419
  • 33
  • 155
  • 245
  • Thank you for answering but I just simply want to see "hello world" on my browser. I think it should work but ... – phucle Oct 14 '11 at 09:18
  • 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 Oct 14 '11 at 09:21
  • @Joe So what about when you use `echo` in cli? – xdazz Oct 14 '11 at 09:25
  • @Joe: Sorry but I tried to change from exec(...) to passthru(...) but still did not work. – phucle Oct 14 '11 at 09:26
  • @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. – Joe Oct 17 '11 at 08:38