0

So I'm trying to use the Symfony process to call a Python script from a controller in my Laravel project.

$process = new Process(['python', 
    'C:\projects\laravel_project\src\public\car_tuner.py', $args]);
$process->run();

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

$result = $process->getOutput();
Log::info($result);

This is the error i am getting:

"The command "'python' 'C:\projects\laravel_project\src\public\car_tuner.py'" failed.\n\nExit Code: 127(Command not found)\n\nWorking directory: /var/www/html/public\n\nOutput:\n================\n\n\nError Output:\n================\nsh: exec: line 1: python: not found\n" trace: [{file: "/var/www/html/vendor/laravel/framework/src/Illuminate/Routing/Controller.php", line: 54,…},…]

Python 3.10 is installed, and running this command in the command prompt works correctly. I've also tried using the PHP shell_exec() function with no success also. Any help is appreciated.

gp_sflover
  • 3,460
  • 5
  • 38
  • 48
  • 1
    Hey Matt, did you try `new Process(['which', 'python']);` already? Do you run your project within a docker container? – Sengorius Apr 27 '22 at 18:57
  • yes my project is in a docker container, will try your suggestion now – Matt Retallick Apr 27 '22 at 20:07
  • Hey Matt, does your container running the laravel project have access to python? I also think you're going to run into problems with the container accessing that absolute path from the host machine. The comment from Sengorius should confirm if python is available to the container. – Nick Lediet Apr 27 '22 at 20:13
  • Having tried that fix i am getting a new error simply stating the command failed: ``` {"exception":"[object] (Symfony\\Component\\Process\\Exception\\ProcessFailedException(code: 0): The command \"'which' 'python' 'C:\\projects\\laravel_project\\src\\public\\car_tuner.py'\" failed. ``` – Matt Retallick Apr 27 '22 at 20:18
  • Well, I guess you just do not have python installed on your docker container, which is running Laravel. You can now either create a new docker image with PHP + Python or try to execute the python tool in its own docker container, e.g. `docker exec -v /path/to/python-project:/usr/src/app python:latest python car_tuner.py` (I'm not a python dev, so this may be incorrect). – Sengorius Apr 28 '22 at 15:35
  • My issue is that I need to call this python script from the controller using parameters passed in from the user in the front end, so i need to be able to call it from the php container, any ideas how i can do that using what i have already? – Matt Retallick Apr 28 '22 at 16:57
  • Any news on this? Struggling at the same point, "python not found", windows terminal and server shows python version, laravel on local, windows machine, doesn't. – FryingPan Jul 06 '23 at 20:43

0 Answers0