0

I've just managed to get mySQLdb for Python installed on OSX - and from the command line it works fine. I can query the database no problem.

What I'm trying to do now is call up a python script from PHP using the system() function.

This snippet:

import MySQLdb

print "hello from py"

when run from PHP system() returns "1" to PHP, but if I run it from the command line it prints out "hello from py". If I comment out the import statement, and run the script through PHP again using system(), it returns "hello from py"

Therefore I'm certain it must be something to do with MySQLdb. I'm not extremely adept at UNIX though I'm guessing it might have something to do with the _www user's access to the mySQLdb module via PHP not being right??

I can't get it to return any errors to PHP so it is hard to troubleshoot.

Some help pointing me in the right direction, either to: get Python's error messages out to PHP, and solve the problem itself, would be appreciated.

EDIT:

Some better information:

I changed the snippet to this:

print "before"

try:
    import MySQLdb
except Exception,e:
    print e

print "hello from py"

And executed it with PHP exec() instead of system:

exec('python /Users/Tapefreak/sites/a/testpy.py');

And this is what came out to PHP:

Can't extract file(s) to egg cache

The following error occurred while trying to extract file(s) to the Python egg cache:

[Errno 13] Permission denied: '/Library/WebServer/.python-eggs'

The Python egg cache directory is currently set to:

/Library/WebServer/.python-eggs

Perhaps your account does not have write access to this directory? You can change the cache directory by setting the PYTHON_EGG_CACHE environment variable to point to an accessible directory.

So, I created the directory and chowned it .

Then, the output of the script changed to:

dlopen(/Library/WebServer/.python-eggs/MySQL_python-1.2.3b2-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib Referenced from: /Library/WebServer/.python-eggs/MySQL_python-1.2.3b2-py2.6-macosx-10.6-universal.egg-tmp/_mysql.so Reason: image not found

This is a message I was receiving earlier, but had thought was fixed -- Turns out it was fixed only for my local user, but not for www and not for root. (bear with me, I'm learning...)

A bit more digging on SO and I found this solution to add a symbolic link: Python mysqldb: Library not loaded: libmysqlclient.18.dylib

which I added in /usr/lib:

ln -s /usr/local/mysql-5.5.16-osx10.6-x86_64/lib/libmysqlclient.18.dylib libmysqlclient.18.dylib

And it appears to be working now: PHP can execute Python that makes use of the MySQLdb.

Community
  • 1
  • 1
Tapefreak
  • 982
  • 1
  • 13
  • 16

2 Answers2

0

Check your apache error log for errors. Also, try su'ing to _www and execute the script manually.

Jake
  • 1,016
  • 8
  • 9
0

Adding the symbolic link in /usr/lib to /usr/local/mysql-5.5.16-osx10.6-x86_64/lib/libmysqlclient.18.dylib as described above fixed this problem for me. Hopefully this will be useful to someone else.

Tapefreak
  • 982
  • 1
  • 13
  • 16