I want to execute a python script,test.py
, from a php function.
#!/usr/bin/env python
import random
def test() :
print("test success")
if __name__ == '__main__':
test()
I can successfully execute it using php command line:
php -r '$message = exec("python3 ~/../scripts/test.py"); print_r($message);'
I've added the following to my php code:
$message = exec("python3 ~/../scripts/test.py", $output, $return_val);
When I execute it, nothing is output (no errors in devtools). When I print the variables, $output
and $message
are empty and $return_val
is 2 (misuse of shell builtins according to Bash documentation).
I know exec()
is allowed after adding the following into the same function:
if (exec('echo TEST') == 'TEST')
{
echo 'exec works!';
}
What could be causing this error? How can I execute the python script with php?