So, I have a python script that uses machine learning models to predict and print a string. Running the script alone gives desired output and takes around 12 seconds to complete. Now calling the python script using a custom wordpress rest api endpoint returns null. I tried running the standard hello world python script, and it works fine. After trying many things and variations, I have discovered that importing the libraries is causing the issue. To be precise importing the numpy.array and/or joblib from sklearn.externals is somehow causing the output to be null. Here is the python code
#!C:\xampp\htdocs\mil\Scripts\python.exe
from datetime import datetime
import json
from numpy import array
from sklearn.externals import joblib
import traceback
print("hello")
And here is the php code I am using inside the custom rest api get function
$topy = 'python C:\xampp\htdocs\mil\mypython\venv\test.py';
$command = escapeshellcmd($topy);
$output = shell_exec($command);
return $output;
I have also tried these 2 alternatives but the problem continues, I get blank string as output. 1.
ob_start();
passthru('python C:\Users\Deepon\PycharmProjects\Home\test.py');
$output = ob_get_clean();
return $output;
2.
$descriptorspec = array(
0 => array("pipe", "r"),
1 => array("pipe", "w"),
2 => array("file", "error-output.txt", "a")
);
$process = proc_open('python C:\xampp\htdocs\mil\mypython\venv\test.py', $descriptorspec, $pipes);
if (is_resource($process)) {
print fgets($pipes[1]);
$return_value = proc_close($process);
return $return_value;
}
So fellas , any ideas on how to solve this dilemna of mine ?