Can’t run Python script from PHP/Laravel

I am building a Laravel application. I want to run a python script within my PHP script.

$process = new Process([
        'C:/Users/kaneo/AppData/Local/Programs/Python/Python38/python',
        'test.py',
      ]);

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

$output = $process->getOutput();
//$output = json_decode($process->getOuput());
dd($output);

I am able to execute commands such as ‘python –version’, and it works, but as soon as I try to run an actual script, I get the following:

Fatal Python error: _Py_HashRandomization_Init: failed to get random numbers to initialize Python Python runtime state: preinitialized

There are some other questions with this same issue but none of them have answers.
Fatal Python error: failed to get random numbers to initialize Python

I also found this resource:
https://www.scivision.dev/python-calling-python-subprocess/

But I don’t understand it.

Source: Symfony Questions

Was this helpful?

0 / 0

Leave a Reply 0

Your email address will not be published. Required fields are marked *