0

I am trying to execute a python script from my PHP file. It was working locally (localhost), the code was:

$command = escapeshellcmd("python " . app_path(). "/http/controllers/machinelearning.py");
$result = shell_exec($command);

However I am getting an error when using python on the server:

shell_exec(): Unable to execute 'python'

If I send:

$command = "which python";
$result = shell_exec($command);

Then I get the echo "/usr/bin/python"

But using the path like:

$command = escapeshellcmd("/usr/bin/python " . app_path(). "/http/controllers/machinelearning.py");
    $result = shell_exec($command);

gives the same error: shell_exec(): Unable to execute 'python'

I also tried passthru:

ob_start();
passthru($command);
$output = ob_get_clean();

Same result: passthru(): Unable to fork [python]

Executing python scripts work in the terminal, so I don't know why these commands don't work when called from the PHP script. Anyone had similar issues? I´ve been trying to find the answer all day.

Chris Fodor
  • 134
  • 15
  • 1
    Did you ask a similar question recently, or was that someone else. Regardless, can you try [`proc_open`](https://stackoverflow.com/a/2320835/231316) instead which will give you access to stderr which might have more insight into the problem. – Chris Haas Nov 02 '21 at 21:39
  • Hello, no, this is my first question about this issue. Thank you for your reply, I tried proc_open and it returned 127, meaning the python is not found within the PATH variable? I am not sure how to fix it – Chris Fodor Nov 03 '21 at 07:46
  • Did you try an absolute path, too? Is `app_path()` absolute? For all of your tests, is this through the web browser, or are you also testing using PHP on the CLI? – Chris Haas Nov 03 '21 at 12:01

0 Answers0