`
Aug 01

PythonI started working on Python start of the year and I learned a lot while I’m doing my internship in my current company, MooWee Inc. So it’s logical for me to try to pick up Django as the web framework.

One of the hurdle I met and everyone else met is trying to install MySQL adapter for Python. Being the most popular database used, it is crucial to have it work rather than circumventing around the problem by using alternative database such as SQLite or PostgreSQL.

After a few hours of searching, I found a few references that somewhat not entirely complete. I did some cross referencing and found two brings me to the entire solutions, which I’m about to show below.

Step 0:
Before I start, I assumed that you have MySQL and Python installed on the mac.

Step 1:
Download the latest MySQL for Python adapter from SourceForge.

Step 2:
Extract your downloaded package by typing

$ tar xzvf MySQL-python-1.2.2.tar.gz

Step 3:
Inside the folder, clean the package by typing

$ sudo python setup.py clean

Step 4:
In the same folder, edit _mysql.c using your favourite text-editor

4a. Remove the following lines (37-39):

#ifndef uint
#define uint unsigned int
#endif

4b. Change the following:

uint port = MYSQL_PORT;
uint client_flag = 0;

to

unsigned int port = MYSQL_PORT;
unsigned int client_flag = 0;

Step 5:
Create a symbolic link under lib to point to a sub-directory called mysql. This is where it looks for during compilation.

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

Step 6:
Edit the setup_posix.py and change the following

mysql_config.path = "mysql_config"

to

mysql_config.path = "/usr/local/mysql/bin/mysql_config"

Step 7:
In the same directory, rebuild your package (ignore the warnings that comes with it)

$ sudo python setup.py build

Step 8:
Install the package and you are done.

$ sudo python setup.py install

Step 9:
Test if it’s working. It works if you can import MySQLdb.

$ python
>>> import MySQLdb

Voila.

References:
n.code
RedElephant
Django users Google Groups

written by mangoorange \\ tags: , , ,


Best Wordpress Hosting Providers - 2014

All three hosts offer FREE 1-click Wordpress installs making them the best Wordpress hosting providers. For more web hosting reviews be sure to check out AlreadyHosting.com.

 #1
#2 
 #3
#1 - Bluehost 
Bluehost Review
#2 - iPage 
iPage Review
#3 - HostMetro 
HostMetro Review


32 Responses to “Installing Python MySQLdb 1.2.2 on Mac OS X”

  1. 1. Tom Weber Says:

    Ah, thanks so much! What a relief for MySQLdb install!

  2. 2. aceplace Says:

    dude, you rock! they need to include this info in the install / readme file!

  3. 3. Emily Says:

    Thank you so much! I spent a few hours searching all over google to try to solve this problem with no luck, but this did it! None of the other instructions I found included all the necessary steps.

  4. 4. Giedrius Says:

    Thanks for the instructuctions! After compiling and installing without errors the test line gave an import error. Any suggestions?
    thanks-
    Giedrius

    >>> import MySQLdb
    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)

  5. 5. Giedrius Says:

    For those falling in the same traps of MySQLdb: with some googling I discovered that above mistake is likely to be due to Mac archtecture incompatibilities between preinstalled Mac Python (32 bit) and other components (MySQL 64 bit etc). After making sure I have 32 bit versions for everything on my iMac (OS 10.5.5), MySQLdb successfully started after reverting to 1.2.1 version (1.2.2 kept giving error in my previous message). ..
    Giedrius

  6. 6. Kearney Says:

    Gledrius,

    I “downgraded” to 1.2.1. but, I had to do some extra steps.

    I modified the _mysql.c to remove the defining of the unit and changed everything from to uint to unsigned int.

    In setup.py I added: /usr/local/mysql/bin/ to the line

    f = popen(“mysql_config –%s” % what)

    so the line now looks like:

    f = popen(“/usr/local/mysql/bin/mysql_config –%s” % what)

    Now, the old packages and eggs need to be deleted. So, I went to /Library/Python/2.5/site-packages, and did an

    sudo rm -rf MySQLdb MySQL_Python-1.2.2-py2.5.egg-info

    then I ran the: python setup.py clean command
    followed by the python setup.py build
    and sudo python setup.py install

    Hope this helps anyone else who was having these problems

  7. 7. David Morton Says:

    Kearney,

    Many thanks for the help. I’ve read elsewhere that people have been “downgrading” to version 1.2.1.

    I followed your instructions, and I got the following error:

    [GCC 4.0.1 (Apple Inc. build 5465)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import MySQLdb
    Traceback (most recent call last):
    File “”, line 1, in
    File “MySQLdb/__init__.py”, line 19, in
    import _mysql
    ImportError: dynamic module does not define init function (init_mysql)

    ——

    would be most grateful if you have any ideas about what’s going on. I went through the process of removing the library files and altering the _mysql.c and setup.py files, and I’m wondering if there’s something else I’m missing.

    Kind regards,
    -David

  8. 8. Michael Kumm Says:

    Thanks – this is what I needed

  9. 9. ds Says:

    Hi, it seems to have worked! THANK YOU! The one quirk is when I import MySQLdb I get the following error:

    Warning (from warnings module):
    File “/Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.2-py2.6-macosx-10.3-i386.egg/MySQLdb/__init__.py”, line 34
    DeprecationWarning: the sets module is deprecated

    I imagine it’s because I’m running Python 2.6 and there is a change in store for the sets module that is called but I’m a newbie and can’t quite figure this out.

    Any ideas?

  10. 10. pvc Says:

    I kept getting the following error even after converting to 32-bit mysql:

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

    Then I discovered that the “clean” option for setup.py does not actually clean out all the old built files so you have to manually delete everything under the “build” directory if you previously built with a 64-bit mysql library.

  11. 11. Darren Says:

    Worked. Thanks!

  12. 12. Sampath Girish Munagala Says:

    Hi Friend,
    I got some problem regarding building ‘setup.py’ file. I am pasting the following error which i’ve got at the time of building and installing file.

    Sampath:MySQL-python-1.2.2 venkatarampey$ sudo python setup.py build
    running build
    running build_py
    copying MySQLdb/release.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb
    running build_ext
    building ‘_mysql’ extension
    gcc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -Dversion_info=(1,2,2,’final’,0) -D__version__=1.2.2 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c _mysql.c -o build/temp.macosx-10.3-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 -DDONT_DECLARE_CXA_PURE_VIRTUAL
    unable to execute gcc: No such file or directory
    error: command ‘gcc’ failed with exit status 1

    Sampath:MySQL-python-1.2.2 venkatarampey$ 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.3-i386/egg
    running install_lib
    running build_py
    copying MySQLdb/release.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb
    running build_ext
    building ‘_mysql’ extension
    gcc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -Dversion_info=(1,2,2,’final’,0) -D__version__=1.2.2 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c _mysql.c -o build/temp.macosx-10.3-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 -DDONT_DECLARE_CXA_PURE_VIRTUAL
    unable to execute gcc: No such file or directory
    error: command ‘gcc’ failed with exit status 1

    How can i solve this??? Please help me out….

    Sampath Girish.M

  13. 13. Heather Says:

    Thank you, thank you, thank you, thank you.

    Thank you.

  14. 14. Stephen Clark Says:

    :-(

    OSX 10.5.6
    Python 2.5.1 (r251:54863, Jan 13 2009, 10:26:13)

    >>> import MySQLdb
    /Library/Python/2.5/site-packages/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg/_mysql.py:3: UserWarning: Module _mysql was already imported from /Library/Python/2.5/site-packages/MySQL_python-1.2.2-py2.5-macosx-10.5-i386.egg/_mysql.pyc, but /Users/stephenkennedy-clark/Desktop/MySQL-python-1.2.2 is being added to sys.path
    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)

  15. 15. Lyle Says:

    I got pages and pages of errors when I tried to build.

    Thanks for trying.

  16. 16. Richard Says:

    Still getting this error:

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

    Traceback (most recent call last):
    File “/Users/me/Applications/python/test/src/test.py”, line 17, in
    import MySQLdb
    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)

    I’ve tried multiple options to get MySQLdb working on 10.5.6:

    http://mysql-python.blogspot.com/2008/03/i-am-not-dead.html

    http://www.mangooranges.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/

    … and I’ve tried Mac Python 3.0.

    I seem to be in a Catch-22 — the version 2.3/2.5 Pythons that comes with 10.5 won’t work, the 3.0 Python can’t find the MySQLdb package at all — and presumably will also miss any other (add-on) local packages I’ve installed.

    Is there any other alternative to dumping out the MySQL data into a suitable flat file format and sucking it back into a python dictionary — primitive, but probably will work for now — though I’ll pay a price every time the data in the database changes.

    Anyone have a MySQLdb fix that works for OS X 10.5.6?

    I admit I’m surprised that python, which has been so robust in having libraries and methods for so many things, hasn’t got something as simple as a working Mac OS X MySQL interface. Heck, even Perl has multiple to chose from.

    Thanks for any assistance …

  17. 17. jordi Says:

    all ok, but the build does not work. Any clue?

    olea:MySQL-python-1.2.2 jvilla$ sudo python setup.py build
    running build
    running build_py
    creating build
    creating build/lib.macosx-10.3-i386-2.5
    copying _mysql_exceptions.py -> build/lib.macosx-10.3-i386-2.5
    creating build/lib.macosx-10.3-i386-2.5/MySQLdb
    copying MySQLdb/__init__.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb
    copying MySQLdb/converters.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb
    copying MySQLdb/connections.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb
    copying MySQLdb/cursors.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb
    copying MySQLdb/release.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb
    copying MySQLdb/times.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb
    creating build/lib.macosx-10.3-i386-2.5/MySQLdb/constants
    copying MySQLdb/constants/__init__.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb/constants
    copying MySQLdb/constants/CR.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb/constants
    copying MySQLdb/constants/FIELD_TYPE.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb/constants
    copying MySQLdb/constants/ER.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb/constants
    copying MySQLdb/constants/FLAG.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb/constants
    copying MySQLdb/constants/REFRESH.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb/constants
    copying MySQLdb/constants/CLIENT.py -> build/lib.macosx-10.3-i386-2.5/MySQLdb/constants
    running build_ext
    building ‘_mysql’ extension
    creating build/temp.macosx-10.3-i386-2.5
    gcc -isysroot /Developer/SDKs/MacOSX10.4u.sdk -fno-strict-aliasing -Wno-long-double -no-cpp-precomp -mno-fused-madd -fno-common -dynamic -DNDEBUG -g -O3 -Dversion_info=(1,2,2,’final’,0) -D__version__=1.2.2 -I/usr/local/mysql/include -I/Library/Frameworks/Python.framework/Versions/2.5/include/python2.5 -c _mysql.c -o build/temp.macosx-10.3-i386-2.5/_mysql.o -g -Os -arch x86_64 -fno-common -D_P1003_1B_VISIBLE -DSIGNAL_WITH_VIO_CLOSE -DSIGNALS_DONT_BREAK_READ -DIGNORE_SIGHUP_SIGQUIT -DDONT_DECLARE_CXA_PURE_VIRTUAL
    In file included from /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/Python.h:57,
    from pymemcompat.h:10,
    from _mysql.c:29:
    /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/pyport.h:761:2: error: #error “LONG_BIT definition appears wrong for platform (bad gcc/glibc config?).”
    In file included from _mysql.c:35:
    /usr/local/mysql/include/my_config.h:1240:1: warning: “SIZEOF_LONG” redefined
    In file included from /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/Python.h:8,
    from pymemcompat.h:10,
    from _mysql.c:29:
    /Library/Frameworks/Python.framework/Versions/2.5/include/python2.5/pyconfig.h:811:1: warning: this is the location of the previous definition
    error: command ‘gcc’ failed with exit status 1

  18. 18. Michael G. Says:

    In what directory (aka “folder”) should I start this process? The download drops the tar.gz file on my desktop, but that seems an odd place to start an installation.

    Thank you!

    Michael

  19. 19. kvh Says:

    f ing brilliant man! thanks!

  20. 20. no Says:

    Simply doesn’t work on 10.5.6

  21. 21. no Says:

    Correction, simply doesn’t work with MAMP. Instructions work fine with 10.5.6 once I junked MAMP.

  22. 22. Clark Says:

    OSX Server 10.5… any ideas. I get the following warning. Is that a bad thing or is this a harmless warning.

    Python 2.6.2 (r262:71600, Apr 16 2009, 09:17:39)
    [GCC 4.0.1 (Apple Computer, Inc. build 5250)] on darwin
    Type “help”, “copyright”, “credits” or “license” for more information.
    >>> import MySQLdb
    /Library/Frameworks/Python.framework/Versions/2.6/lib/python2.6/site-packages/MySQL_python-1.2.2-py2.6-macosx-10.3-fat.egg/MySQLdb/__init__.py:34: DeprecationWarning: the sets module is deprecated
    >>>

  23. 23. Clark Says:

    http://sourceforge.net/forum/message.php?msg_id=5808948

    This link fixed my issue above.

    It’s also good to make sure that if you build your own version of Python that you update all of the SymLinks.

  24. 24. laptop ekran Says:

    thank you good share

  25. 25. ygarten Says:

    This was a great posting, and ultimately the instructions on this page saved me:
    http://birdhouse.org/blog/2009/02/21/python-mysql-connections-on-mac-os/

    Explanation:
    I had the same problem (errors) as David Morton, after following Kearny’s advice.

    I tried downgrading to 1.2.2, then 1.2.1 using that advice, and still got:
    >>> import MySQLdb
    Traceback (most recent call last):
    File “”, line 1, in
    File “MySQLdb/__init__.py”, line 19, in
    import _mysql
    ImportError: dynamic module does not define init function (init_mysql)

    Finally, found that this error:
    Re: ImportError: module does not define init function (init_mysql)

    As reported elsewhere by Leon Harris, gives clues towards using darwinports to fix it:

    “I was stuck with this error for absolutely ages. I found that using the py25-mysql.darwinports.com port of python mysql connector fixed the problem. Which was occurring due to a problematic _mysql.so file in site-packages.”

    Googled that, and found solution by following:

    http://birdhouse.org/blog/2009/02/21/python-mysql-connections-on-mac-os/

    And it works!!!

  26. 26. One mistake and you have to support it for the rest of your life. (Michael Sinz) » Blog Archive » Mac OS X te MySQLdb 1.2.2 kurmak Says:

    […] http://www.mangooranges.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/ Tags: django, mysql, MySQLdb, python […]

  27. 27. aspratley Says:

    If you’re having trouble getting it installed the new version of mysql-python requires some different instructions, also if you’re using 64 or 32 bit versions of python or mysql will make a difference. There’s some information here:

    http://learninglamp.wordpress.com/2010/02/21/mysqldb-python-mysql-and-os-x-a-match-made-in-satans-bum/

  28. 28. Python – MySQL (via MAMP sous Mac OS X) Says:

    […] Installing Python MySQLdb 1.2.2 on Mac OS X […]

  29. 29. Django Tutorials index « Roshan Book Says:

    […]  Installing Python MySQLdb 1.2.2 on Mac OS X Leopard […]

  30. 30. MySQL-python (aka MySQLdb) on OSX Lion | Active Intelligence Says:

    […] Installing Python MySQLdb 1.2.2 on Mac… – mangooranges.com […]

  31. 31. Communication between FrontlineSMS and Website « AgNetPro Says:

    […] was the use of the MAMP software as a web server, as the software only seemed to work with MacPort. http://www.mangooranges.com/2008/08/01/installing-python-mysqldb-122-on-mac-os-x/. It appeared that the problem exist with GCC compilers version 3.0 and 4.0 […]

  32. 32. Makhate Says:

    For me this is what it says:
    /Users/username/Downloads/MySQL-python-1.2.4b4/distribute-0.6.28-py2.7.egg
    sh: mysql_config: command not found
    Traceback (most recent call last):
    File “setup.py”, line 18, in
    metadata, options = get_config()
    File “/Users/username/Downloads/MySQL-python-1.2.4b4/setup_posix.py”, line 43, in get_config
    libs = mysql_config(“libs_r”)
    File “/Users/username/Downloads/MySQL-python-1.2.4b4/setup_posix.py”, line 25, in mysql_config
    raise EnvironmentError(“%s not found” % (mysql_config.path,))
    EnvironmentError: mysql_config not found

Leave a Reply

You must be logged in to post a comment.