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
  • hillscottc
    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" ?
  • 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!
  • muanis
    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.
  • Your link to download MySQL 5.0.41 is dead.
    You can get the tarball from http://downloads.mysql.com/archives.php?p=mysql...
  • Paul B. Hartzog
    http://www.newtonslore.com/2007/06/09/mysqldb-l...

    will get you through the egg errors
  • 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
  • Emily
    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?
  • pvc
    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.
  • kevinh
    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.
  • gprx
    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)
    >>>
  • Eric
    When you say:

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

    What was the '...' part?

    Thanks
  • 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 /* */
  • christoffer
    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.
  • joel
    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)
    >>>
  • Wim
    Thanks for sharing,
    Wim.
  • akaalias
    Awesome, thank you very much for the post, compiling worked like a charm after the edit and linking.
  • Mogga
    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?
  • Worked perfectly. Thanks for sharing!
blog comments powered by Disqus