How to call Python file from PHP?

WBOY
Release: 2023-09-24 15:30:02
forward
1105 people have browsed it

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;
?>
Copy after login

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
Copy after login

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;
?>
Copy after login

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!

source:tutorialspoint.com
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!