How to get MySQLdb to work on OSX with _www user?
<p>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.</p>

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

<p>This snippet: </p>

<pre><code>import MySQLdb

print "hello from py"
</code></pre>

<p>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 <em>comment out the import statement</em>, and run the script through PHP again using system(), it returns "hello from py"</p>

<p>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??</p>

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

<p>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.</p>

<p><strong>EDIT:</strong></p>

<p>Some better information:</p>

<p>I changed the snippet to this:</p>

<pre><code>print "before"

try:
    import MySQLdb
except Exception,e:
    print e

print "hello from py"
</code></pre>

<p>And executed it with PHP exec() instead of system:</p>

<pre><code>exec('python /Users/Tapefreak/sites/a/testpy.py');
</code></pre>

<p>And this is what came out to PHP:</p>

<blockquote>
  <p>Can't extract file(s) to egg cache</p>
  
  <p>The following error occurred while trying to extract file(s) to the
  Python egg cache:</p>
  
  <p>[Errno 13] Permission denied: '/Library/WebServer/.python-eggs'</p>
  
  <p>The Python egg cache directory is currently set to:</p>
  
  <p>/Library/WebServer/.python-eggs</p>
  
  <p>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.</p>
</blockquote>

<p>So, I created the directory and chowned it .</p>

<p>Then, the output of the script changed to:</p>

<blockquote>
  <p>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</p>
</blockquote>

<p>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...)</p>

<p>A bit more digging on SO and I found this solution to add a symbolic link:  <a href="http://stackoverflow.com/questions/6383310/python-mysqldb-library-not-loaded-libmysqlclient-18-dylib/6967816#6967816">Python mysqldb: Library not loaded: libmysqlclient.18.dylib</a></p>

<p>which I added in /usr/lib:</p>

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

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