Home  >  Article  >  Backend Development  >  How to call Python file from PHP?

How to call Python file from PHP?

WBOY
WBOYforward
2023-09-24 15:29:021057browse

How to call Python file from PHP?

To call a Python file from a PHP file, you need to call it using the shell_exec function.

For example

<?php
    $command = escapeshellcmd(&#39;/usr/custom/test.py&#39;);
    $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.

For example

<?php
    $command = escapeshellcmd(&#39;python3 /usr/custom/test.py&#39;);
    $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!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete