How to run a Python file in PHP?
There are several ways to execute a Python file in PHP, below are two common methods:
- Execute a Python file using the exec() function.
$pythonScript = 'python /path/to/your/python/script.py';
exec($pythonScript, $output);
print_r($output);
- Execute a Python file using the shell_exec() function.
$pythonScript = 'python /path/to/your/python/script.py';
$output = shell_exec($pythonScript);
echo $output;
Ensure that the Python interpreter is installed on the server and that the path to the Python file is correct when running the Python file. Also, pay attention to the execution permissions for PHP to ensure that external commands can be executed.