我想从php函数执行python脚本 我可以使用php命令行成功执行它: p>
我在PHP代码中添加了以下内容: p>
执行时,不输出任何内容(devtools中没有错误)。 当我打印变量时, 我知道在将以下内容添加到同一个函数后, 可能导致此问题 错误? 如何用php执行python脚本? p>
div> test.py code>。 p>
#!/ usr / bin / env python
import random
def test():
print(“test success”)
if __name__ == '__main __':
test()
code> pre>
php -r'$ message = exec(“python3~ /../ scripts / test.py”); 的print_r($消息);”
code> pre>
$ message = exec(“python3~ /../scripts/test.py“,$ output,$ return_val);
code> pre>
$ output code>和
$ message code>为空,
$ return_val code>为2(根据Bash文档滥用shell builtins )。 p>
exec() code>是允许的: p>
if( exec('echo TEST')=='TEST')
{
echo'exec works!';
}
code> pre>
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?