Running python script in Laravel Controller: “sh: 1: python: not found ”

╄→尐↘猪︶ㄣ 提交于 2021-01-28 19:46:30

问题


I am trying to run a python script in my Laravel PHP controller using Symfony Process. The python file is located in the public directory. The python file is:

import requests

URL = 'someURL.com'

response = request.get(url);

print(response)

Here is my code in my PHP Controller:

    $process = new Process('python '.public_path('MyPython.py'));

    $process->run();

    //executes after the command finishes
    if (!$process->isSuccessful()) {
        throw new ProcessFailedException($process);
    }

    Log::error($process->getOutput());

When I run it I get this:

"message": "The command "python /var/www/project/public/MyPython.py" failed. Exit Code: 127(Command not found) Working directory: /var/www/project/public Output: ================ Error Output: ================ sh: 1: python: not found ",

While troubleshooting I created another python script called 'Simply.py' that just prints out "Hello World". When I run:

 $process = new Process('python '.public_path('Simply.py'));

The same error persists. But when I run:

 $process = new Process('python3 '.public_path('Simply.py'));

It is successful! I then try to run:

 $process = new Process('python3 '.public_path('MyPython.py'));

But then I get a new type of error, which is:

"message": "The command "python3 /var/www/project/public/MyPython.py" failed. Exit Code: 1(General error) Working directory: /var/www/project/public Output: ================ Error Output: ================ Traceback (most recent call last): File "/var/www/Matchban/public/MyPython.py", line 1, in <module> import requests ModuleNotFoundError: No module named 'requests' "

How can I either get it to run using the python command not python3, or how can I import the request module in my laravel project?

来源:https://stackoverflow.com/questions/61894525/running-python-script-in-laravel-controller-sh-1-python-not-found

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!