Menu Close

How to run a Python script from PHP?

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.

Posted in Python, Python Answers