Asked  2 Years ago    Answers:  5   Viewed   225 times

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.



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;


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

#!/usr/bin/env python

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.

To make executable a file on unix-type platforms:

chmod +x
Saturday, August 6, 2022

First of all, what is the version of PHP?

If other .php scripts work except for phpMyAdmin, the is pretty safe to assume something is wrong with phpMyAdmin installation. (either with the files or the Virtual Host)


  1. Can you run another script in phpMyAdmin directory? Try with a file containing phpinfo(); If it does not work then the problem is in your apache configuration.

  2. If the above is true, then the parser is not recognising phpMyAdmin/index.php as a php file, for some reason. Try reinstalling phpMyAdmin.


Remove AddType application/x-httpd-php .php. You should not need it.

In PHP 5.4 magic_quotes_gpc was removed. If you're running PHP5, remove that line.

As of PHP 4.0.3, track_vars is always turned on, so remove it also.

As of PHP 4.2.0, register_globals this directive defaults to off. In PHP5.3 register_globals is deprecated, as of PHP5.4 register_globals was removed. SO if running PHP > 4.2.0 remove that line.

Wednesday, December 14, 2022
for f in *.csv; do
  python "$f" "${f%.csv}list.txt"

Will that do the trick? This will put foo.csv in foolist.txt and abc.csv in abclist.txt.

Or do you want them all in the same file?

Tuesday, September 27, 2022

The equivalent of apt-get update in Arch Linux is pacman -Syy.

pacman -Syu is equivalent to apt-get update && apt-get upgrade.

Might as well make this complete if it stays...

pacman -S <package> is apt-get install <package>

Friday, October 7, 2022

Using compile

I've come up with a solution using the built-in function compile, as follows.

Contents of the file

with open('') as f:
    source_code =
compiled = compile(
    filename='', mode='exec', optimize=2)

Contents of the file

if __debug__:
    print('Debug ON')
    print('Debug OFF')

The output from running python is:

Debug OFF

Possible values for the parameter optimize:

  • -1: use same optimization level as the Python interpreter that is running the function compile
  • 0: no optimization, and __debug__ == true
  • 1: like -O, i.e., removes assert statements, and __debug__ == false
  • 2: like -OO, i.e., removes also docstrings.

Don't know if it's the best option, just sharing if can be useful fo others.


The subprocess-based approach is still more concise, and can be made portable by using sys.executable:

import subprocess
import sys

if not sys.executable:
    raise RuntimeError(sys.executable)
proc =
    [sys.executable, '-OO', ''],
    capture_output=True, text=True)
if proc.returncode != 0:
    raise RuntimeError(proc.returncode)

The above code calls the function

The check for the value of the variable sys.executable is motivated by the documentation of CPython:

If Python is unable to retrieve the real path to its executable, sys.executable will be an empty string or None.

The check is implemented with a raise statement, instead of an assert statement, in order to check also in cases that the above Python code is itself run with optimization requested from Python, e.g., by using python -O or python -OO or the environment variable PYTHONOPTIMIZE.

When optimization is requested, assert statements are removed.

Using raise statements also enables raising an exception other than AssertionError, in this case RuntimeError.

For running Python code that is within a function inside the same source file (i.e., inside, not inside, the function inspect.getsource can be used, together with the option -c of python.

By the way better answers are welcome!

Monday, August 15, 2022
Only authorized users can answer the search term. Please sign in first, or register a free account.
Not the answer you're looking for? Browse other questions tagged :

Browse Other Code Languages