I am running Laravel Homestead 5.7. have a command script for a python program which runs perfectly when I execute it through the terminal, however when I enter the command into php's exec function, it always fails.
I believe it might have something to do with the wrong version of python being called at runtime. I can run "python --version" in my terminal, which gives me version 2.7, however in php's exec function, it just returns a blank line.
First, I would suggest you check your app rights to execute that script and access the directory where the script is in. For example, PHP runs as a www-data group if it is under Apache, etc. Also, make sure the script is executable by applying chmod +x *.py.
You can test both shell_exec() & exec(). The first one will "execute a command via shell and return the complete output as a string". Maybe that is what you really want. https://www.php.net/manual/en/function.shell-exec.php
You can use a shebang like #!/usr/bin/env python on the first line of your python script to specify your python environment. But #!/usr/local/bin/python2.7 would help you to specify the exact version you want to run.
@DIEGOAURINO - The host is Windows 10, and the Homestead box is running through Virtualbox.
I've checked the user that exec() uses and it is "vagrant", which is the same user that I log in as through the terminal, and the script has execute privileges.
The error log indicates that python can't determine os.environ["PATH"] and throws a KeyError which makes me think it is a PATH issue but I don't understand why running it through my terminal would work when it's logged in as the same user.
Is the problem solved? Where the script is located exactly? Check the file ownership with stat -c "%U %G" *.py to see if there is something strange.
One important thing is that the shell runs based on your .profile/.bashrc settings, your application does not, even when the user is the same in this case. It also means that your "PATH" is not accessible from the application when you run a script directly from the app. What you can do in this case is to run a subprocess from your user. But it is not a good practice since you are inside a web server.
Another point is that os.environ["PATH"] works differently in Windows. Thus, make sure your text editor is not kidding you. Check for proper imports and quotes.