Calling python script from laravel using symfony process component

832 Views Asked by At

currently im working on project that require me to use python and laravel .I've searched a lot in the internet and apparently the solution to call python function is to use symphony. Im pretty sure that i already tried everything and somehow it doesn't work

Controller

--first try (Using Symfony):

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class AdminController extends Controller
{
    function PythonRun()
    {
        $process = new Process(['/usr/bin/python3', 'test.py']);
        $process->run();
        
        if (!$process->isSuccessful()) {
            throw new ProcessFailedException($process);
        }
    
        $data = $process->getOutput(); 
        dd($data);      
    }
}

-- Second try (Using Exec):

    class AdminController extends Controller
{
    function PythonRun()
    {       
       exec("/usr/bin/python3 /python/test.py");       
    }
}

Both of them return " The system cannot find speciefied path". i already tried to put the python in the app folder and in the public folder. both doesnt work.

1

There are 1 best solutions below

1
On

There is no problem with what you want to do and you can run the Python program with any of the possible methods such as exec or using Symfony / process.

But before doing this, you must be sure of things like permissions, etc. for the current user who executes the exec or process command.