6

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!";
   }
8
  • Did you enable error reporting? Commented Mar 24, 2013 at 3:46
  • 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. Commented Mar 24, 2013 at 3:48
  • 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 Commented Mar 24, 2013 at 3:50
  • 1
    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?? Commented Mar 24, 2013 at 4:06
  • @Mike the exec() is enabled. Commented Mar 24, 2013 at 4:09

4 Answers 4

15

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.

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

5 Comments

the point i am making over here is that I tried running a simple script through the exact function success() and it worked. 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??
In addition, make sure exec is allowed.
PHP has some kind of limit, like 30sec, you may easily find from calling phpinfo() and searching for max_execution_time in the output. it's possible to change this from the script to a higher value, if you need, using ini_set('max_execution_time', 1200);, and you may get another details about max_execution_time from this question: stackoverflow.com/questions/4220413/…
If I recall correctly (which I may not), when executing external commands or sleep it doesn't count that towards execution time except on Windows boxes.
I had to do: which python and then use: /usr/local/bin/python
1

try to use exact path to the python program.

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

2 Comments

It is in the same repository/folder..directly accessible.
exec('/usr/bin/python testing.py');
0

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);
}

5 Comments

Good eye, but I don't think it would actually affect it: codepad.viper-7.com/KT2UDx
@khan what is the script returning? You can store the output to check... exec('python testing.py', $output) - I updated the answer to show what I am talking about.
@doitlikejustin I think var_dump would be preferred in this case
I can make the script to return some thing, but the point is I have just executed a small python script with this and it got executed. It is only this script (which takes a huge amount of time to get executed) is actually not being executed from php exec. My suspicion is on the PHP that it is not executing the script for some memory size limitation or something else, because the script takes at least 20+ seconds to completely execute itself.
Set a time limit of 60 seconds then... php.net/manual/en/function.set-time-limit.php
0

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");

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.