Run python script from larave framework

851 Views Asked by At

Hi everyone who i can i pass laravel parameter with symfony process code i use it to run python script

use Symfony\Component\Process\Process; 
use Symfony\Component\Process\Exception\ProcessFailedException; 
 
$process = new Process(['python3','/path/to/your_script.py',$policyname); 
$process->run();
1

There are 1 best solutions below

0
ssbhattarai On

yes you can pass laravel parameter to python.

  1. You need to install symfony/process using following command:

    composer require symfony/process
    
  2. you can use it in laravel as

    use Symfony\Component\Process\Process;
    use Symfony\Component\Process\Exception\ProcessFailedException;
    
    $process = new Process(['python', '/path/to/script.py']);
    $process->run();
    
    // error handling
    if (!$process->isSuccessful()) {
       throw new ProcessFailedException($process);
    }
    
    $output_data = $process->getOutput();
    

If you want to pass parameters to python, do this:

$process = new Process(['python', '/path/to/script.py', $arg]);