6
votes

I have a simple PHP function that is supposed to execute a Pyton script when its called. I have tried this sort of function multiple times in my php programs, but somehow this time this function is not executing the python script at all. When I access the script from the command prompt and run python testing.py then it successfully gets executed. One thing that I want to mention that this script has some serious implementations of python's NLTK library, and takes more than 20 seconds to execute and perform its operations (i.e. data process and storing to db). Is this delay in the execution which is causing this problem or is there something else that I am missing this time?

function success(){
   $mystring = exec('python testing.py');
   $mystring;
   if(!$mystring){

        echo "python exec failed";
            }
   else{
   echo "<br />";
   echo "successfully executed!";
   }
4
Did you enable error reporting? - Mike
yes, but nothing happened. It keeps echoing that python exec failed (as it is supposed to do). But once I go into command line and execute the script, it gets executed. - khan
It is very common that exec is disabled in the php config file. Try checking out this question first to see if that is the case: stackoverflow.com/questions/3938120/check-if-exec-is-disabled - Mike
You should update your question to the actual problem, which you stated in a comment to @lenik's answer: Its more about this script and the amount of time it takes to process i.e. more than 20 seconds. So, My question actually is that is there some feature in php that it waits for the script to be executed for a certain fixed period of time and then moves forward regardless of the script's execution success or failure?? - Burhan Khalid
@Mike the exec() is enabled. - khan

4 Answers

13
votes

you have to use full path for the python and for your file. you may find the former from the which python command, that most likely outputs '/usr/bin/python' and you should already know the latter. so your command would look like this:

$mystring = exec('/usr/bin/python /home/user/testing.py');

and you should make sure your python script has all appropriate permissions, because your web-server most probably is running as a different user, so permissions should be "-rwxrwxr-x" or something close.

1
votes

try to use exact path to the python program.

$mystring = exec('python testing.py');
0
votes

Try removing the $mystring; line

function success() {
   $mystring = exec('python testing.py');
   if(!$mystring){
       echo "python exec failed";
   } else {
       echo "<br />";
       echo "successfully executed!";
   }
}

For testing purposes try:

function success() {
   $mystring = exec('python testing.py', $output);
   var_dump($output);
}
0
votes

There is no issue with the exec() or anything.
The problem is that the nltk module is not able to locate the nltk_data directory. For it just locate where the nltk_data is present in your system: usually ~/nltk_data.
Now import add that path when you run the function.
import nltk;
Now, nltk.data.path is a list of locations where to search for the modules.
You can just do nltk.data.path.append("your location/directory");