Fix Python – Running a Python script from PHP


Asked By – Abandoned account

I’m trying to run a Python script from PHP using the following command:

exec('/usr/bin/python2.7 /srv/http/assets/py/ arg1 arg2');

However, PHP simply doesn’t produce any output. Error reporting is set to E_ALL and display_errors is on.

Here’s what I’ve tried:

  • I used python2, /usr/bin/python2 and python2.7 instead of /usr/bin/python2.7
  • I also used a relative path instead of an absolute path which didn’t change anything either.
  • I tried using the commands exec, shell_exec, system.

However, if I run

if (exec('echo TEST') == 'TEST')
    echo 'exec works!';

it works perfectly fine while shutdown now doesn’t do anything.

PHP has the permissions to access and execute the file.

EDIT: Thanks to Alejandro, I was able to fix the problem. If you have the same problem, don’t forget that your webserver probably/hopefully doesn’t run as root. Try logging in as your webserver’s user or a user with similar permissions and try to run the commands yourself.

Now we will see solution for issue: Running a Python script from PHP


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 error
occurred or the command produces no output.


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


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

#!/usr/bin/env python

If you have several versions of Python installed, /usr/bin/env will
ensure 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 indicate
what 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 not
apply (but that “shebang line” does no harm, and will help if you ever
copy 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 and then running it directly: ./,
rather than just python

To make executable a file on unix-type platforms:

chmod +x

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 a
unix-type platform and can’t seem to get it to work. PHP executes as
the web user on the system (generally www for Apache), so you need to
make sure that the web user has rights to whatever files or
directories that you are trying to use in the shell_exec command.
Other wise, it won’t appear to be doing anything.

This question is answered By – Alejandro Quiroz

This answer is collected from stackoverflow and reviewed by FixPython community admins, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0