简体   繁体   English

使用symfony进程组件从laravel调用python脚本

[英]Calling python script from laravel using symfony process component

currently im working on project that require me to use python and laravel.I've searched a lot in the inte.net and apparently the solution to call python function is to use symphony.目前我正在从事需要我使用 python 和 laravel 的项目。我在 inte.net 中搜索了很多,显然调用 python function 的解决方案是使用交响乐。 Im pretty sure that i already tried everything and somehow it doesn't work我很确定我已经尝试了所有方法但不知何故它不起作用

Controller Controller

--first try (Using Symfony): --第一次尝试(使用 Symfony):

use Symfony\Component\Process\Process;
use Symfony\Component\Process\Exception\ProcessFailedException;

class AdminController extends Controller
{
    function PythonRun()
    {
        $process = new Process(['/usr/bin/python3', 'test.py']);
        $process->run();
        
        if (!$process->isSuccessful()) {
            throw new ProcessFailedException($process);
        }
    
        $data = $process->getOutput(); 
        dd($data);      
    }
}

-- Second try (Using Exec): -- 第二次尝试(使用 Exec):

    class AdminController extends Controller
{
    function PythonRun()
    {       
       exec("/usr/bin/python3 /python/test.py");       
    }
}

Both of them return " The system cannot find speciefied path".他们都返回“系统找不到指定的路径”。 i already tried to put the python in the app folder and in the public folder.我已经尝试将 python 放入应用程序文件夹和公共文件夹中。 both doesnt work.两者都不起作用。

There is no problem with what you want to do and you can run the Python program with any of the possible methods such as exec or using Symfony / process.您想做什么没有问题,您可以使用任何可能的方法运行 Python 程序,例如 exec 或使用 Symfony / process。

But before doing this, you must be sure of things like permissions, etc. for the current user who executes the exec or process command.但在这样做之前,你必须确定当前执行exec或process命令的用户的权限等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM