Python Script Failing to Execute from PHP exec()

后端 未结 4 1912
死守一世寂寞
死守一世寂寞 2020-12-19 10:08

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 th

相关标签:
4条回答
  • 2020-12-19 10:38

    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 讨论(0)
  • 2020-12-19 10:42

    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.

    0 讨论(0)
  • 2020-12-19 10:42

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

    0 讨论(0)
  • 2020-12-19 10:43

    try to use exact path to the python program.

    $mystring = exec('python testing.py');
    
    0 讨论(0)
提交回复
热议问题