Categories
Python Answers

How to run a Python script from PHP?

Spread the love

Sometimes, we want to run a Python script from PHP.

In this article, we’ll look at how to run a Python script from PHP.

How to run a Python script from PHP?

To run a Python script from PHP, we can use the shell_exec function.

For instance, we write

$command = escapeshellcmd('/usr/custom/test.py');
$output = shell_exec($command);
echo $output;

to call escapeshellcmd to escape the command string.

Then we call shell_exec to run the $command.

And we get the output from $output.

Conclusion

To run a Python script from PHP, we can use the shell_exec function.

By John Au-Yeung

Web developer specializing in React, Vue, and front end development.

Leave a Reply

Your email address will not be published. Required fields are marked *