0

So I have a php script in which I send the required data from app and then in php script, that data is passed as command line arguments to a python script. The issue is when I run python script from the terminal, It runs successfully but when I use postman to run the PHP script, the python script doesn't run.

$output = shell_exec("python3 final.py $day $month $year $hour 2>&1"); This is the line where I am calling the py script, when I run the same from terminal, it produces the output but when I run it like this and dump $output, I get the error

ModuleNotFoundError: No module named 'sklearn'

However sklearn is Installed already, and its working via terminal.

Kindly suggest what should I do regarding this issue ?

zukayu
  • 101
  • 1
  • 9
  • sounds like a case of multiple python installs perhaps and the wrong one is being fired? I'd suggest removing your script, and running a "Debugging" script instead. Something that prints out the sys.path information and so on. – Paritosh Singh Dec 24 '19 at 16:30
  • I installed sklearn for python3 only, and not python2 therefore it should work :( but ain't working ... it can't be because of version – zukayu Dec 24 '19 at 16:38
  • 1
    Exactly, you installed it, but it's not working! Surely, there's some magic involved here somewhere, right?! Or, it's time to test your assumptions. I am not saying that you assume what i've said is correct, But when your programs tell you something that you didn't think was possible, it's a good time to start testing things. – Paritosh Singh Dec 24 '19 at 16:41
  • Well I made a sample.py script with only a print statement and that just prints the command line args I passed via the PHP script. It doesn't have sklearn imported. That script just runs fine ... But this script having sklearn don't run. – zukayu Dec 24 '19 at 16:55
  • 1
    print out this info: `import sys; print(sys.executable)`. [reference](https://stackoverflow.com/questions/2589711/find-full-path-of-the-python-interpreter). See if both paths match when you run via php and normally. – Paritosh Singh Dec 24 '19 at 17:42
  • @ParitoshSingh I added the line print(f"Exe : {sys.executable}") and ran it both on terminal and via php and both the times I got the same answer i.e `/usr/bin/python3` and its working on terminal but not via php. I can't get the issue, why its showing no module named sklearn :( – zukayu Dec 25 '19 at 03:54

1 Answers1

1

So its working now, ran the command as sudo and used exec() instead of shell_exec() and also changed the user for execution of the command, reference this https://raspberrypi.stackexchange.com/a/78156 .

You might need to bypass password for the user, Bypass it for www-data see this https://stackoverflow.com/a/24107529/10498074

Also see the jenkins answer on the same page https://stackoverflow.com/a/24648413/10498074

My final command is $output = exec("sudo -S -u username /usr/bin/python3 /var/www/html/PathTo/final.py $day $month $year $hour 2>&1");

and in sudo visudo, I added the command at the last line www-data ALL =(ALL) NOPASSWD: /usr/bin/python3

Tried to run the php script again, and it worked !

zukayu
  • 101
  • 1
  • 9