要从 php 文件中调用 python 文件,您需要使用 shell_exec 函数来调用它。
例如<?php $command = escapeshellcmd('/usr/custom/test.py'); $output = shell_exec($command); echo $output;?>
这将调用脚本。但在顶部的脚本中,您还需要指定解释器。因此,在您的 py 文件中,在顶部添加以下行:
#!/usr/bin/env python
或者,您也可以在执行命令时提供解释器。
例如<?php $command = escapeshellcmd('python3 /usr/custom/test.py'); $output = shell_exec($command); echo $output;?>
以上就是如何从php中调用python文件?的详细内容。