Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Execute Python script using Laravel?

I'm trying to execute a python script in a Laravel 5.8 project but I'm having problems with the Symfony/process class.

Basically, I want to run this python script that takes an excel form from the storage folder.

My first try was this

$process = new Process('C:\Python\python.exe C:\Users\"my path"\laravel\storage\app\images\cargaExcel.py');

$process->run();

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

echo $process->getOutput();

And the error is

Fatal Python error: _Py_HashRandomization_Init: failed to get random numbers to initialize Python

I also tried with shell_exec(), and if the two files (the excel and the python script are in the public path - app/public) it works.

I think the problem is that python only executes on the app/public folder, so I don't know how to run this in another path.

Python output is telling me that:

Working directory: C:\Users\"my path"\laravel\public

Does anyone know how to run this?

like image 346
RafaelM Avatar asked Aug 15 '26 09:08

RafaelM


1 Answers

You can pass the working directory as the second argument of the process class

So it can be:

$process = new Process('C:\Python\python.exe C:\Users\"my path"\laravel\storage\app\images\cargaExcel.py', "/my/working/path/");

Also you may pass command as array

$process = new Process(['C:\Python\python.exe', 'C:\Users\"my path"\laravel\storage\app\images\cargaExcel.py'], "/my/working/path/");
like image 171
Riccardo Venturini Avatar answered Aug 18 '26 07:08

Riccardo Venturini



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!