Python和PHP / MAMP

I recently got python working with MAMP in the terminal. The below script works in the terminal and prints what it should:

#!/usr/bin/python
import MySQLdb

db = MySQLdb.connect (unix_socket = '/Applications/MAMP/tmp/mysql/mysql.sock',
                    host = 'localhost',
                    user = '*****',
                    passwd = '****',
                    db = 'Database')

cur = db.cursor() 

cur.execute("SELECT * FROM TABLE")

for row in cur.fetchall() :
    print row[0]
    print row[1]

However it will not work when I call it via PHP:

exec("python test.py",$output);
echo $output[0] 

I get the following PHP error

Notice: Undefined offset: 0 in /test.php on line 7

I created second test python script as the below:

import MySQLdb
print 'worked'

And it works as expected - but only when I have

import MySQLdb 

commented out.

Any suggestions?