0

I have a Python script that is using MySQLdb on OSX Lion. To get MySQLdb to work, I had to add

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib:$DYLD_LIBRARY_PATH

to my ~/.bash_profile. The python script, when executed from the command line, works just fine.

When I try executing the same python script from a PHP script using the exec() function, the python script is unable to locate the library path. Here's the err msg from the Apache log:

ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Library/Python/2.7/site-packages/_mysql.so
  Reason: image not found

I'm assuming this is a user issue though I'm unclear on how to fix it.

Where should I put this path so that it will be globally available to all scripts and users on my machine? (well at least, to Apache and Root)

1
  • Some more info/clarification: If I run python from terminal as a normal, non-root user, "import MySQLdb" works; if I sudo and try the same, I get the error. Therefore the problem must be one of scope for the user. I would like to solve this outside of Python by exporting the DYLD_LIBRARY_PATH in the right place to be available to all users, but I don't know where/how to do this.
    – Tapefreak
    Feb 17, 2012 at 1:14

3 Answers 3

1

Solution: creating a soft link did the trick:

sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib

This solution was taken from comments on this post. Perhaps this solution will help another struggling with MySQLdb on OSX.

Though I'm still not clear on why it worked on one user and not another - or, rather, to which file the above mentioned export should have been saved in order to make it available to all. Any explanation on this would be appreciated.

1
  • Apache doesn't read .bash_profile, so it doesn't pick up the environment variable.
    – user149341
    Mar 12, 2012 at 20:18
0

It looks like you need to modify sys.path array. It initialized from the environment variable PYTHONPATH: http://docs.python.org/library/sys.html#sys.path

1
  • No, there is a solution outside of python. I had this same code working on another dev machine (Snow Leopard). I just need some help w/users/permissions in UNIX...
    – Tapefreak
    Feb 17, 2012 at 2:42
0

For Apache you can add PYTHONPATH entry to the EnvironmentVariables section in /System/Library/LaunchDaemons/org.apache.httpd.plist that will make it available to anything Apache runs. See man launchd.plist for details.

For root you can add an export PYTHONPATH to /etc/profile which will make it available to all sh and bash login shells. See man bash for details.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.