问题
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