Running a Python script from PHP

✔ Recommended Answer

Tested on Ubuntu Server 10.04. I hope it helps you also on Arch Linux.

In PHP use shell_exec function:

Execute command via shell and return the complete output as a string.

It returns the output from the executed command or NULL if an erroroccurred or the command produces no output.

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

Into Python file test.py, verify this text in first line: (see shebang explain):

#!/usr/bin/env python

If you have several versions of Python installed, /usr/bin/env willensure the interpreter used is the first one on your environment's$PATH. The alternative would be to hardcode something like#!/usr/bin/python; that's ok, but less flexible.

In Unix, an executable file that's meant to be interpreted can indicatewhat interpreter to use by having a #! at the start of the first line,followed by the interpreter (and any flags it may need).

If you're talking about other platforms, of course, this rule does notapply (but that "shebang line" does no harm, and will help if you evercopy that script to a platform with a Unix base, such as Linux,Mac, etc).

This applies when you run it in Unix by making it executable(chmod +x myscript.py) and then running it directly: ./myscript.py,rather than just python myscript.py

To make executable a file on unix-type platforms:

chmod +x myscript.py

Also Python file must have correct privileges (execution for user www-data / apache if PHP script runs in browser or curl)and/or must be "executable". Also all commands into .py file must have correct privileges.

Taken from php manual:

Just a quick reminder for those trying to use shell_exec on aunix-type platform and can't seem to get it to work. PHP executes asthe web user on the system (generally www for Apache), so you need tomake sure that the web user has rights to whatever files ordirectories that you are trying to use in the shell_exec command.Other wise, it won't appear to be doing anything.

Method #2

To run a Python script from PHP, you can use the exec() function in PHP. Here's an example:

php
<?php $script_path = '/path/to/your/python/script.py'; $argument1 = 'argument1_value'; $argument2 = 'argument2_value'; $result = exec("python3 $script_path $argument1 $argument2"); echo $result; ?>

In this example, replace /path/to/your/python/script.py with the path to your Python script, and $argument1 and $argument2 with the arguments you want to pass to your Python script.

The exec() function runs the command specified as its parameter and returns the output of the command as a string. In this case, the command is the Python script with its arguments, and the output of the command is stored in the $result variable.

Note that you need to have Python installed on your server to be able to run Python scripts.

Comments

Most Popular

Remove Unicode Zero Width Space PHP

PhpStorm, return value is expected to be 'A', 'object' returned

Laravel file upload returns forbidden 403, file permission is 700 not 755