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

View Comments 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 [...]

pvc (Jan 26th):

The _mysql.so ImportError on init_mysql puzzeled my for a while until I discovered that “python setup.py clean” does not do a complete job of discarding the prior build. If you previously built against a 64 bit library of mysql you need to manually delete everything below the build directory before doing a new build with the 32 bit version.

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” ?

air jordan 10 (Jun 5th):

Well , the view of the passage is totally correct ,your details is really reasonable and you guy give us valuable informative post, I totally agree the standpoint of upstairs. I often surfing on this forum when I m free and I find there are so much good information we can learn in this forum! http://spoon8.net/

UGG boots (Jun 11th):

I am also a XX fan who really like this! I also like XX, and purchase lots of it every time,
<a href=” http://www.eeadie.com“ >eeadie
like-minded friends can have a look ,we can communicate by the way~~

I am agree with landlord, many of us are not too concerned about sports news. As the World Cup. Many of us are also don't know what it is.I believe once you go to see some sports news,you will love sports.By the way ,I know some websites content is very well.But I don’t know how to choose.Can you help me?The website are Air force 1 high

air force 1 shoes (Jun 25th):

The post of content is very interesting and exciting. I learned a lot from here.The content from simple to complex, so all of you can come in . No matter you want to see what can Nike air force be found

air jordan 13 (Jul 5th):

Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
Gucci sweaters
If its common it might be difficult to get, however, be creative and you can usually find something.

discount uggs (Jul 14th):

Mark S. is definitely on the right track. If you want to get a professional looking email address, Id recommend buying your name domain name, like or
ajf 10
If its common it might be difficult to get, however, be creative and you can usually find something.

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.
blog comments powered by Disqus