5

Nov

Filed in Code, How-To's, Mac, MySQL |

As many people probably have, I ran across the problem of installing MySQLdb (Python) on Leopard today.

The error:

In file included from /usr/local/mysql/include/mysql.h:43,
from _mysql.c:40:
/usr/include/sys/types.h:92: error: duplicate 'unsigned'
/usr/include/sys/types.h:92: error: two or more data types
in declaration specifiers
error: Setup script exited with error: command 'gcc' failed
with exit status 1

Luckily, there are people out there who know a lot more than me about these things, and someone had already found a fix:

To get mysqldb working on leopard I found I had to edit _mysql.c and edit out the lines:

#ifndef uint
#define uint unsigned int
#endif

I also had to edit site.cfg and mark threadsafe to False.

After doing this, I managed to get MySQLdb to install, great! Not so fast, there was yet another problem:

Referenced from: .../_mysql.so
Reason: image not found

The solution:

sudo ln -s /usr/local/mysql/lib/ /usr/local/mysql/lib/mysql

21 Responses to "MySQLdb on Leopard"

Subscribe to this topic with RSS or get the Trackback URL
Dave Fowler (Dec 30th):

Worked perfectly. Thanks for sharing!

Mogga (Jan 25th):

This is one of the more frustrating builds I’ve come across in recent memory…
Everything build fine but it still doesn’t work…
I keep getting the following error

{{{
ImproperlyConfigured: Error loading MySQLdb module: dlopen(/var/tmp/.python_eggs/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg-tmp/_mysql.so, 2): no suitable image found. Did find:
/var/tmp/.python_eggs/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg-tmp/_mysql.so: no matching architecture in universal wrapper
}}}

any ideas?

[...] you need to edit the download MySQLdb sources file _mysql.c (as per this ) and remove the following lines near the top#ifndef uint #define uint unsigned int [...]

Alexis (Apr 3rd):

Awesome, thank you very much for the post, compiling worked like a charm after the edit and linking.

Wim (Apr 20th):

Thanks for sharing,
Wim.

joel (Jun 10th):

I am still getting this error :(

>>> import MySQLdb
Traceback (most recent call last):
File “”, line 1, in
File “build/bdist.macosx-10.5-i386/egg/MySQLdb/__init__.py”, line 19, in
File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 7, in
File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_mysql)
>>>

christoffer (Jun 26th):

Thanks for the great tips! After following the guide that you provided, I ran into the same problem as joel.

I finally got it to work this morning, by using the older version of MySQLdb (1.2.1). Seems that there is no way (at least none that I found) to fix this with version 1.2.2.

Justin Lilly (Aug 30th):

Great writeup. Spent a few seconds trying to figure out why it wasn’t working. For those as tired as I am, the proper commenting is /* */

Eric (Sep 17th):

When you say:

Referenced from: …/_mysql.so
Reason: image not found

What was the ‘…’ part?

Thanks

gprx (Oct 1st):

Still getting this error:

Traceback (most recent call last):
File “”, line 1, in
File “MySQLdb/__init__.py”, line 19, in
import _mysql
File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 7, in
File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_mysql)
>>>

kevinh (Oct 3rd):

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

You have a 64bit mysql package … and a 32bit python package.

Either get the 64bit python package … or the 32bit mysql package and rebuild the module.

Emily (Nov 10th):

I'm having the same ImportError problem as well, but I've checked that mysql, python, and _mysql.so in the MySQLdb egg are all 32-bit. Everything I've found googling this problem has indicated the same solution that's been suggested here: that the architectures don't match. Any other suggestions or anything else that might be 64-bit and causing the problem?

indiehead (Dec 29th):

Great work, unfortunately didn’t work this end, any ideas?

indiehead:MySQL-python-1.2.2 indiehead$ sudo python setup.py install
running install
running bdist_egg
running egg_info
writing MySQL_python.egg-info/PKG-INFO
writing top-level names to MySQL_python.egg-info/top_level.txt
writing dependency_links to MySQL_python.egg-info/dependency_links.txt
reading manifest file ‘MySQL_python.egg-info/SOURCES.txt’
reading manifest template ‘MANIFEST.in’
writing manifest file ‘MySQL_python.egg-info/SOURCES.txt’
installing library code to build/bdist.macosx-10.5-i386/egg
running install_lib
running build_py
copying MySQLdb/release.py -> build/lib.macosx-10.5-i386-2.5/MySQLdb
running build_ext
building ‘_mysql’ extension
creating build/temp.macosx-10.5-i386-2.5
gcc -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DMACOSX -I/usr/include/ffi -DENABLE_DTRACE -pipe -Dversion_info=(1,2,2,’final’,0) -D__version__=1.2.2 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c _mysql.c -o build/temp.macosx-10.5-i386-2.5/_mysql.o -g -Os -arch i386 -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT
_mysql.c:37: error: syntax error before ‘uint’
_mysql.c:38: error: syntax error before ‘unsigned’
In file included from /usr/local/mysql/include/mysql.h:47,
from _mysql.c:40:
/usr/include/sys/types.h:84: error: syntax error before ‘typedef’
_mysql.c:485:3: error: invalid preprocessing directive #uint
_mysql.c:486:3: error: invalid preprocessing directive #uint
error: command ‘gcc’ failed with exit status 1

[...] David Cramer.net – MySQLdb on Leopard [...]

[...] -Conrad I tried this a couple of weeks ago using macports and had problems. See, for example: http://www.davidcramer.net/code/57/m…n-leopard.html I read about several people who went in and edited the source files to work out the bugs on [...]

Paul B. Hartzog (Apr 22nd):
Kévin Dunglas (Apr 24th):

Your link to download MySQL 5.0.41 is dead.
You can get the tarball from http://downloads.mysql.com/archives.php?p=mysql-5.0&v=5.0.41

muanis (Jun 13th):

I’ve got the same problem.

Running mysql 5.1.35. After discovering that I should change to 32 bit version, repeated all the steps and got the same error about the init function.

>>> import MySQLdb
import sys, pkg_resources, imp
Traceback (most recent call last):
File “”, line 1, in
File “MySQLdb/__init__.py”, line 19, in
import _mysql
File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 7, in
File “build/bdist.macosx-10.5-i386/egg/_mysql.py”, line 6, in __bootstrap__
ImportError: dynamic module does not define init function (init_mysql)

After a couple of hours debugging I noticed that the dist and build dirs of MySQLdb 1.2.2 were not removed when I issue a python setup.py clean.

I manually removed both of them and got everything working fine.

ssteinerX (Dec 31st):

I had the same problem, then noticed that I was installing with ’sudo’ while building as my regular user.

See: http://websaucesoftware.com/blog/?p=461 and part 2 at http://websaucesoftware.com/blog/?p=462

No need to change to 32 bit mode and screw everything up, just install the one with the right architecture!

hillscottc (Jan 10th):

I am also totally frustrated with
ImportError: dynamic module does not define init function (init_mysql)

@ssteinerX , I see you got it fixed, but what you actually did isn’t clear to me. I also don’t want to ‘downgrade’ to 32 bit. Can you please explain “just install the one with the right architecture” ?

Leave A Reply

 Username (*required)

 Email Address (*private)

 Website (*optional)

Note: Comments moderation may be active so there is no need to resubmit your comment.