0

I'm trying to run a Python script within my laravel application which processes a file and stores it in a specific directory. Laravel should use that processed file further.

When running a local Laravel server using php artisan serve and call the following php code, it's not working:

I've tested the following:

$process = new Process(['python', '--version']);
$process->run();

if(!$process->isSuccessful()) {
    throw new ProcessFailedException($process);
}

return $process->getOutput();

For an unknown reason, Laravel throws me a ProcessFailedException:

The command "python --version" failed. Exit Code: 1(General error) Working directory: D:\Path\To\Project\public Output: ================ Error Output: ================ Der Befehl "python" ist entweder falsch geschrieben oder konnte nicht gefunden werden.

which means that command "python" could not be found.

I'm using a Windows 11 machine for developing and Linux server which is used for production later. I've installed Python 3.11.4 on my Windows and when I'm typing python --version inside terminal, it shows me the correct Python version. So I guess Python is installed correctly on my local machine.

Something else that confuses me is that when I'm providing the full path of Python (installed at D:\Python\python.exe), it works: $process = new Process(['D:\Python', '--version']); // Output: Python 3.11.4

Another thing is, that running shell_exec('python --version') // Output: Python 3.11.4 (instead of using symfony/process) works as well.

It's also working on my Linux server without providing full path.

Anyone could tell me why it's not working using symfony/process? Why it's saying python coul0d not be found and everything else works?

I'm out of ideas. Thank you.

FryingPan
  • 81
  • 2
  • "It's also working on my Linux server without providing full path." - By that you mean `shell_exec`, right? When you tested that on your Linux server, did you just testing over SSH/CLI, or did you test through your web browser. The general short answer to the problem is that not everything is installed globally always, so you sometimes need to specify an absolute path, or come up with another way for the process to resolve the executable's location – Chris Haas Jul 06 '23 at 22:01
  • I've tested both on my Linux server, shell_exec and symfony with "python --version" and it worked both. I've completely reinstalled python on my windows and also ticked the checkmark to add python to PATH variable but it still doesnt work in Laravel. – FryingPan Jul 08 '23 at 11:08

0 Answers0