Current Location: Home> Latest Articles> How to Execute Python Scripts from PHP for Seamless Integration

How to Execute Python Scripts from PHP for Seamless Integration

gitbox 2025-06-13

Installing Python

First, ensure that Python is installed. Python is usually pre-installed on Linux and macOS systems, while on Windows, it needs to be downloaded and installed manually. You can check if Python is installed correctly by running the following command:

python --version

If the command outputs the Python version number, the installation was successful.

Writing a Python Script

Next, write a Python script. For example, let’s say we want to calculate the next number in the Fibonacci sequence. Here’s a simple Python script:

def fib(n):
    if n <= 1:
        return n
    else:
        return fib(n-1) + fib(n-2)
<p>print(fib(10))

This script will compute the 10th Fibonacci number and print the result.

Calling Python Scripts from PHP

We can use PHP's `exec()` function to call Python scripts. Here’s a PHP example that runs a Python script and captures its output:

$output = exec("python /path/to/fib.py");
echo "output: " . $output;

In this example, the `exec()` function runs the specified Python script and stores the output in the `$output` variable, which is then printed using `echo`.

Passing Arguments to Python Scripts

If you need to pass arguments from PHP to the Python script, you can specify them in the `exec()` function. For example, to calculate the 20th Fibonacci number, modify the PHP code as follows:

$output = exec("python /path/to/fib.py 20");
echo "output: " . $output;

In the Python script, you can use `sys.argv` to access the arguments passed from PHP. Here’s the updated Python script:

import sys
<p>def fib(n):<br>
if n <= 1:<br>
return n<br>
else:<br>
return fib(n-1) + fib(n-2)</p>
<p>n = int(sys.argv[1])<br>
print(fib(n))

In this example, `sys.argv` captures the argument passed from PHP and converts it to an integer.

Handling Return Values from Python

If your Python script produces multiple lines of output, you can use PHP's `explode()` function to split the output string into an array and process it line by line. Here’s an example of how to handle multiple lines of output in PHP:

$output = exec("python /path/to/fib.py 20");
$lines = explode("\n", $output);
foreach ($lines as $line) {
    echo "output: " . $line . "";
}

Installing Python Libraries for Use in PHP

If you need to use external Python libraries, you can use the `exec()` function in PHP to call `pip install` and install the required libraries. Here’s an example that installs and uses the `requests` library:

exec("sudo pip install requests");
$output = exec("python /path/to/my_script.py");

In this example, we first install the `requests` library using `pip`, and then use it in the Python script.

Security Considerations

When using PHP's `exec()` function to run Python scripts, it’s important to be aware of security concerns. Attackers might attempt to inject malicious code via the `exec()` function. It is crucial to sanitize user input and adopt best practices, such as using `shell_exec()` instead of `exec()`, and avoid placing Python scripts in the web root directory where they can be accessed by malicious users.

Conclusion

This article discussed how to call Python scripts from PHP. By using the `exec()` function, developers can easily run Python scripts and capture the output. We also covered how to pass arguments, handle return values, install Python libraries, and some security considerations. This method is useful when integrating PHP and Python for various functionalities in web development.