我可以尝试在Laravel中运行Python脚本。我使用composer require symfony/process来执行此操作。实际上,我不想使用shell_exec()
。当我尝试运行它时,返回错误。在命令行中,一切正常。我的Python脚本位于公共文件夹中。
我的控制器:
use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;
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脚本:
print('Hello Python')
错误:
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:\Users\User\Desktop\www\abyss-hub\public
Output: ================ Error
Output: ================ Fatal
Python error:
_Py_HashRandomization_Init: failed to get random numbers to initialize Python
Python runtime state: preinitialized.
2条答案
按热度按时间jw5wzhpr1#
使用Symfony进程。https://symfony.com/doc/current/components/process.html安装:
代码:
5vf7fwbs2#
您需要配置进程环境变量:
https://symfony.com/doc/current/components/process.html#setting-environment-variables-for-processes