Laravel: Fatal Python error: _Py_HashRandomization_Init: failed to get random numbers to initialize Python Python runtime state: preinitialized
Question:
I can try to run Python script in Laravel. I am using composer require symfony/process for this. Actually, I don’t want to use shell_exec()
. When I try to run it, an error is returned. In the command line, everything is okay. My python script is located in the public folder.
My Controller:
use SymfonyComponentProcessProcess;
use SymfonyComponentProcessExceptionProcessFailedException;
public function plagiarismCheck() {
$process = new Process(['C:/Program Files/Python39/python.exe', 'C:/Users/User/Desktop/www/abyss-hub/public/helloads.py']);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return $process->getOutput();
}
Python script:
print('Hello Python')
Error:
The command ""C:/Program Files/Python39/python.exe" "C:/Users/User/Desktop/www/abyss-hub/public/helloads.py"" failed.
Exit Code: 1(General error)
Working directory:
C:UsersUserDesktopwwwabyss-hubpublic
Output: ================ Error
Output: ================ Fatal
Python error:
_Py_HashRandomization_Init: failed to get random numbers to initialize Python
Python runtime state: preinitialized.
Answers:
You need to configure the process environment variables:
https://symfony.com/doc/current/components/process.html#setting-environment-variables-for-processes
$process = new Process(["python", $pathToFilePython, ...$yourArgs], env: [
'SYSTEMROOT' => getenv('SYSTEMROOT'),
'PATH' => getenv("PATH")
]);
$process->run();
$process->getOutput();
- PATH: To recognize the "python "command.
- SYSTEMROOT: OS dependencies for Python to work.
Use Symfony Process. https://symfony.com/doc/current/components/process.html
Install:
composer require symfony/process
Code:
use SymfonyComponentProcessProcess;
use SymfonyComponentProcessExceptionProcessFailedException;
$process = new Process(['python', '/path/to/your_script.py']);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();
I can try to run Python script in Laravel. I am using composer require symfony/process for this. Actually, I don’t want to use shell_exec()
. When I try to run it, an error is returned. In the command line, everything is okay. My python script is located in the public folder.
My Controller:
use SymfonyComponentProcessProcess;
use SymfonyComponentProcessExceptionProcessFailedException;
public function plagiarismCheck() {
$process = new Process(['C:/Program Files/Python39/python.exe', 'C:/Users/User/Desktop/www/abyss-hub/public/helloads.py']);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
return $process->getOutput();
}
Python script:
print('Hello Python')
Error:
The command ""C:/Program Files/Python39/python.exe" "C:/Users/User/Desktop/www/abyss-hub/public/helloads.py"" failed.
Exit Code: 1(General error)
Working directory:
C:UsersUserDesktopwwwabyss-hubpublic
Output: ================ Error
Output: ================ Fatal
Python error:
_Py_HashRandomization_Init: failed to get random numbers to initialize Python
Python runtime state: preinitialized.
You need to configure the process environment variables:
https://symfony.com/doc/current/components/process.html#setting-environment-variables-for-processes
$process = new Process(["python", $pathToFilePython, ...$yourArgs], env: [
'SYSTEMROOT' => getenv('SYSTEMROOT'),
'PATH' => getenv("PATH")
]);
$process->run();
$process->getOutput();
- PATH: To recognize the "python "command.
- SYSTEMROOT: OS dependencies for Python to work.
Use Symfony Process. https://symfony.com/doc/current/components/process.html
Install:
composer require symfony/process
Code:
use SymfonyComponentProcessProcess;
use SymfonyComponentProcessExceptionProcessFailedException;
$process = new Process(['python', '/path/to/your_script.py']);
$process->run();
// executes after the command finishes
if (!$process->isSuccessful()) {
throw new ProcessFailedException($process);
}
echo $process->getOutput();