To call a Python file from a PHP file, you need to call it using the shell_exec function.
<?php $command = escapeshellcmd('/usr/custom/test.py'); $output = shell_exec($command); echo $output; ?>
This will call the script. But in the script at the top, you also need to specify the interpreter. So, in your py file, add the following line at the top:
#!/usr/bin/env python
Alternatively, you can also provide an interpreter when executing the command.
<?php $command = escapeshellcmd('python3 /usr/custom/test.py'); $output = shell_exec($command); echo $output; ?>
The above is the detailed content of How to call Python file from PHP?. For more information, please follow other related articles on the PHP Chinese website!