7

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?

5
  • Check this stackoverflow.com/questions/41020068/… Commented Mar 15, 2019 at 12:59
  • how about $cwd = getcwd(); chdir(new wordking direcotry); run python; chdir($cwd) ? Commented Mar 15, 2019 at 13:49
  • @apokryfos thanks, that's the way to solve. Commented Mar 26, 2019 at 16:44
  • 1
    Possible duplicate of Running python script in Laravel, How to run Python Script in Laravel?, etc. Commented Jul 22, 2019 at 17:02
  • create a virtual environment venv of python & run that python file path in virtual env from instance of process class Commented Feb 5, 2021 at 16:47

1 Answer 1

1

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/");
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.