Python Script Failed to execute from PHP exec ()

I have a simple PHP function that should execute a Pyton script when it is called. I tried this function several times in my php programs, but somehow this time this function does not execute the python script at all. When I call the script from the command line and run python testing.py , it succeeds. One thing I want to mention is that this script has some serious implementations of the python NLTK library and takes more than 20 seconds to complete and execute its operations (i.e. the data processing and storage in db). Is this a delay in execution that causes 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
source share
4 answers

you need to use the full path for python and for your file. you can find the first from which python command, which most likely displays "/ usr / bin / python", and you should already know the latter. so your command will look like this:

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

and you have to make sure that your python script has all the appropriate permissions, because your web server most likely works as a different user, so the permissions should be "-rwxrwxr-x" or something close.

+8
source

try using the exact path to the python program.

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

Try removing the string $mystring;

 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
source

No problem with exec () or anything.
The problem is that the nltk module cannot find the nltk_data directory. To do this, simply find where nltk_data is present on your system: usually ~ / nltk_data.
Now import this path when the function starts.
import nltk;
Now nltk.data.path is a list of places to search for modules.
You can simply execute nltk.data.path.append ("your location / directory");

0
source

All Articles