Debugging a world of hell when trying to install MySQL-Python on Mac OS X

I've been trying for weeks and weeks to install MySQL-python with absolutely no luck, on Mac OS X 10.6 with Python 7.0 from the Enthought Python Distribution. Many of the issues I ran into have been described in other posts and I'm pretty sure I read all of those, but still cannot fix this issue. I tried installing and reinstalling various MySQL packages for 64bit OS X but finally just compiled it from scratch. MySQL, as far as I know, works and lives in the directory /usr/local/mysql-5.5.9-osx10.6-x86/.

I was able to install MySQL-python-1.2.3 using "sudo setup.py install". However, when I import it, I get the error:

$ python
Enthought Python Distribution -- www.enthought.com
Version: 7.0-2 (64-bit)

Python 2.7.1 |EPD 7.0-2 (64-bit)| (r271:86832, Dec  3 2010, 15:56:20) 
[GCC 4.0.1 (Apple Inc. build 5488)] on darwin
Type "help", "copyright", "credits" or "license" for more information.
>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.5-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>

  File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so
  Reason: image not found

I looked to see, as other posters on here suggested, what _mysql.so is referencing using otool:

$ otool -L /Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so
/Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so:
    libmysqlclient.18.dylib (compatibility version 18.0.0, current version 18.0.0)
    /usr/lib/libSystem.B.dylib (compatibility version 1.0.0, current version 125.2.1)
    /usr/lib/libgcc_s.1.dylib (compatibility version 1.0.0, current version 438.0.0)

The correct file seems to exist here:

$ ls -al /usr/local/mysql-5.5.9-osx10.6-x86/lib/libmysqlclient.16.dylib
-rwxr-xr-x  1 root  wheel  3539752 Jan 20  2011 /usr/local/mysql-5.5.9-osx10.6-x86/lib/libmysqlclient.16.dylib

I tried several approaches to make _mysql.so recognize this file:

  1. Making symbolic links, as others suggested, to the right file:

    $ sudo ln -s /usr/local/mysql-5.5.9-osx10.6-x86/lib/libmysqlclient.16.dylib /usr/lib/libmysqlclient.18.dylib

when I do this, I get a new error:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.5-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>

  File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so
  Reason: no suitable image found.  Did find:
    /usr/lib/libmysqlclient.18.dylib: mach-o, but wrong architecture
  1. Setting DYLD_LIBRARY_PATH, as follows (after removing the previous symbolic link):

    $ export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql-5.5.9-osx10.6-x86/lib/

Then I get the same error as initially:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.5-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>

  File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 7, in <module>
  File "build/bdist.macosx-10.5-x86_64/egg/_mysql.py", line 6, in __bootstrap__
ImportError: dlopen(/Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
  Referenced from: /Users/foo/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.5-x86_64.egg-tmp/_mysql.so
  Reason: image not found
  1. I also tried as Ned suggested to use install_name_tool to modify the reference

    from ~/.python-eggs directory:

    trying to change 18 to 16: $ sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.9-osx10.6-x86/libmysqlclient.16.dylib _mysql.so

    approach 2: trying to change 18 to 18: sudo install_name_tool -change libmysqlclient.18.dylib /usr/local/mysql-5.5.9-osx10.6-x86/lib/mysql/libmysqlclient.18.dylib _mysql.so

When I try these, I get the error:

>>> import MySQLdb
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "build/bdist.macosx-10.5-x86_64/egg/MySQLdb/__init__.py", line 19, in <module>

ImportError: dynamic module does not define init function (init_mysql)

Any ideas on what can be done to fix this? I'd really like to get this package working... I tried for a while and could not get it to work. I understand it's a common issue but I could not get any of the posted solutions to work.

Thanks very much for your help.


First I would open a terminal and:

python <enter>
import sys <enter>
print sys.path <enter>

Is your relevant library on the path?

On the MacPorts theory (@fideli above) there is also another PMS called Homebrew Link. It works surprisingly well for package management on Mac.

However, I have done this (fully compiled) in the past with added pain that I was doing it on a G5 PPC64. The way I got it to work was to compile Python, Python-MySQL, MySQL, and mod_wsgi all from source. It is possible but the major pitfall that folks run into is architecture mismatches between packages. Here are my notes from doing it on a PPC. Extract from it what you can and if you need extra help please let us know. The key part to pay attention to is export the ARCHFLAGS before each compilation. This will ensure you have an architecture match. As I said the below is what I did for a PPC64 environment however you can just replace the archs with your relevant platform e.g. x86_64 since you're running 10.6 we know you have an Intel chip. You can ignore the exact software versions as they shouldn't be all that relevant. I hope this is useful for you.

Sidenote: It appears your MySQL is 32bit but your Python is 64bit.

Upfront Details:

  • The target platform was OSX 10.5.8 (PPC64).
  • The goal was to compile all distributions as "Fat" binaries meaning it will support x86, PPC, PPC64, and x86_64.
  • Manipulate this as you see fit for your target platform.

This file will go through the steps of how to
install Python, MySQL, MySQLdb-Python, and
mod_wsgi on clean MacOSX 10.5.8 all 64Bit
#################################################
1.Download Python Source -> Plan 2.7
2.Download MySQL Source -> Plan 5.1.50
3.Download MySQLdb-Python Source -> Plan 1.2.3
4.Download mod_wsgi -> Plan 1.3.3

Untar all of the above and prep for Python
#################################################
1. export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
2. ./configure --with-framework-name=Python64 --with-universal-archs=all --enable-framework --enable-universalsdk=/ MACOSX_DEPLOYMENT_TARGET=10.5
3. make
4. sudo make install
5. Add to /etc/paths
-> /usr/local/bin
6. Modify /etc/profile
-> if [ -x /usr/libexec/path_helper ]; then
-> PATH=""
-> eval `/usr/libexec/path_helper -s` <- Be sure to note these are backticks ie Shell expansion
-> fi
7. Reboot the system
8. Test Python Intereter is @2.7

Compile and Install mod_wsgi
#################################################
1. export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
2. ./configure --with-apxs=/usr/sbin/apxs --with-python=/usr/local/bin
3. make
4. sudo make install
5. Add to /etc/apache2/httpd.conf
-> LoadModule wsgi_module libexec/apache2/mod_wsgi.so
6. tail -f /var/log/apache2/error_log
7. sudo apachectl stop
8. sudo apachectl start

Compile and Install MySQL
#################################################
NOTE: It has been said to also set CFLAGS="-arch i386 -arch ppc -arch ppc64 -arch x86_64"
NOTE: It has been said to also set CXXFLAGS="-arch i386 -arch ppc -arch ppc64 -arch x86_64"
1. export ARCHFLAGS='-arch ppc64'
2. CFLAGS="-arch ppc64' CXXLAGS="-arch ppc64' ./configure -prefix=/usr/local/mysql -with-server-suffix=-standard -enable-thread-safe-client -enable-local-infile -enable-shared -with-zlib-dir=bundled -with-big-tables -with-readline -with-plugins=all -without-docs
3. make
4. sudo make install
5. cd /usr/local/mysql
6. sudo ./bin/mysql_install_db --user=mysql
7. sudo chown -R mysql ./var
8. create file com.mysql.mysqld.plist

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>KeepAlive</key>
<true/>
<key>Label</key>
<string>com.mysql.mysqld</string>
<key>Program</key>
<string>/usr/local/mysql/bin/mysqld_safe</string>
<key>RunAtLoad</key>
<true/>
<key>UserName</key>
<string>mysql</string>
<key>WorkingDirectory</key>
<string>/usr/local/mysql</string>
</dict>
</plist>

9. sudo mv ~/Desktop/com.mysql.mysqld.plist /Library/LaunchDaemons
10. sudo chown root /Library/LaunchDaemons/com.mysql.mysqld.plist
11. sudo launchctl load -w /Library/LaunchDaemons/com.mysql.mysqld.plist
12. edit /etc/paths
-> /usr/local/mysql/bin
13. Quit all running terminals
14. run mysql_secure_installation

Compile and Install MySQLdb-Python
#################################################
NOTE: According to the compilation flags above it "should" be safe to leave Threadsafe at true
NOTE: setuptools must be installed prior to attempting the below
1. Set Threadsafe=False in site.cfg
2. export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
3. python setup.py build
4. sudo python setup.py install
5. python-all <-Universal app, python is aliased to python32 so the next step will fail
6. import MySQLdb
7. hopefully no errors

0 comments
October 4, 2010 by Chris Barker
Django Deployment on Leopard PPC64 Research
See Step By Step HOWTO.txt
#################################################

Links that have proven useful
#################################################
http://tickletik.blogspot.com/2008/12/python-and-mysql-on-64bit.html <-Best all inclusive article
http://mysql-python.blogspot.com/2008/03/i-am-not-dead.html
http://mail.python.org/pipermail/pythonmac-sig/2008-June/020123.html
http://www.stereoplex.com/2008/feb/5/mac-os-x-command-and-manual-search-path-path-and-m/
http://www.softec.lu/site/DevelopersCorner/MasteringThePathHelper
http://code.google.com/p/modwsgi/wiki/QuickInstallationGuide
http://code.google.com/p/modwsgi/wiki/QuickConfigurationGuide
http://diymacserver.com/installing-mysql/compiling-mysql-51x-in-64-bits-mode-on-leopard/
http://mysql-python.sourceforge.net/FAQ.html

Native MacOSX Leopard Bit Levels
#################################################
-Native Python (2.51) 32Bit
-Native Apache (2.2.14) 64Bit
-Native MySQL (N/A)

Build Out Python 64bit
#################################################
To Perform Universal Compilation
Execute the below prior to compiling
export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
./configure --with-framework-name=Python64 --with-universal-archs=all --enable-framework --enable-universalsdk=/ MACOSX_DEPLOYMENT_TARGET=10.5

Adjusting The Path
------------------
Add /usr/local/bin to the top of /etc/paths
Modify /etc/profile to:
if [ -x /usr/libexec/path_helper ]; then
PATH=""
eval `/usr/libexec/path_helper -s`
fi


Build Out MySQL 64Bit
#################################################
CFLAGS="-arch ppc64" <-Perhaps not needed if export ARCHFLAGS is set first
CXXFLAGS="-arch ppc64" <-Perhaps not needed if export ARCHFLAGS is set first
export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
./configure -prefix=/usr/local/mysql -with-server-suffix=-standard -enable-thread-safe-client -enable-local-infile -enable-shared -with-zlib-dir=bundled -with-big-tables -with-readline -with-plugins=all -without-docs
make
sudo make install

Build Out MySQLdb
#################################################
Set Threadsafe=False in site.cfg
export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
python setup.py build
sudo python setup.py install

Build Out Apache 64Bit ??? Or Not
#################################################
No need to rebuild apache, it is native universal binary (Runs in 64bit Navtively)

Vhost notes
-----------
<VirtualHost *:80>

ServerName www.example.com
ServerAlias example.com
ServerAdmin [email protected]

DocumentRoot /usr/local/www/documents

Alias /robots.txt /usr/local/www/documents/robots.txt
Alias /favicon.ico /usr/local/www/documents/favicon.ico

Alias /media/ /usr/local/www/documents/media/

<Directory /usr/local/www/documents>
Order allow,deny
Allow from all
</Directory>
WSGIPythonHome /Path/to/Python/
WSGIDaemonProcess example.com processes=2 threads=15 display-name=%{GROUP}
WSGIProcessGroup example.com
WSGIScriptAlias / /usr/local/www/wsgi-scripts/myapp.wsgi

<Directory /usr/local/www/wsgi-scripts>
Order allow,deny
Allow from all
</Directory>

</VirtualHost>

HTTPD.conf notes
----------------
LoadModule wsgi_module libexec/apache2/mod_wsgi.so


Build Out mod_wsgi
#################################################
export ARCHFLAGS='-arch i386 -arch ppc -arch ppc64 -arch x86_64'
./configure --with-apxs=/usr/sbin/apxs --with-python/path/to/python
make
sudo make install

Cleaning Up After Compiling
#################################################
make clean
make distclean

Enthought EPD actually does have MySQLdb available, it's just not installed by default. To install it:

enpkg MySQL_python

This isn't well documented. Here are all the optional packages: http://www.enthought.com/products/repository.php Good luck!