Search code examples
osx-mountain-lionmysql-python

Unable to install python-mysql on Lion/Mountain Lion


I spent two full days trying to get python-mysql working on my clean install of Lion last year and in the end gave up in despair. Having upgraded to Mountain Lion I thought I would give it another try but with no success. It may be that all my attempts with Lion have screwed up my settings and my only hope is a clean install, but just in case anyone can rescue me, here are the symptoms.

First I deleted the three versions still in existence:

/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-i386.egg-tmp
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-i386.egg-tmp/_mysql.so
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-intel.egg-tmp/_mysql.so
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-x86_64.egg-tmp
/Users/phoebebr/.python-eggs/MySQL_python-1.2.3-py2.7-macosx-10.7-x86_64.egg-tmp/_mysql.so

Then reinstalled. All appears to go ok, though with warnings, and seems to be 64 bit version:

    Phoebe-Brights-iMac:.python-eggs phoebebr$ sudo pip install mysql-python
    dyld: DYLD_ environment variables being ignored because main executable (/usr/bin/sudo) is setuid or setgid
    Password:
    Downloading/unpacking mysql-python
      Downloading MySQL-python-1.2.4c1.zip (113kB): 113kB downloaded
      Running setup.py egg_info for package mysql-python
        Downloading http://pypi.python.org/packages/source/d/distribute/distribute-0.6.28.tar.gz
        Extracting in /tmp/tmp7fFOxU
        Now working in /tmp/tmp7fFOxU/distribute-0.6.28
        Building a Distribute egg in /private/tmp/pip-build/mysql-python
        /private/tmp/pip-build/mysql-python/distribute-0.6.28-py2.7.egg

    Installing collected packages: mysql-python
      Running setup.py install for mysql-python
        building '_mysql' extension
        clang -fno-strict-aliasing -fno-common -dynamic -g -Os -pipe -fno-common -fno-strict-aliasing -fwrapv -mno-fused-madd -DENABLE_DTRACE -DMACOSX -DNDEBUG -Wall -Wstrict-prototypes -Wshorten-64-to-32 -DNDEBUG -g -Os -Wall -Wstrict-prototypes -DENABLE_DTRACE -pipe -Dversion_info=(1,2,4,'rc',5) -D__version__=1.2.4c1 -I/usr/local/mysql/include -I/System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7 -c _mysql.c -o build/temp.macosx-10.8-intel-2.7/_mysql.o -Os -g -fno-common -fno-strict-aliasing -arch x86_64
        clang: warning: argument unused during compilation: '-mno-fused-madd'
        In file included from _mysql.c:44:
        /usr/local/mysql/include/my_config.h:330:11: warning: 'SIZEOF_SIZE_T' macro redefined
          #define SIZEOF_SIZE_T  SIZEOF_LONG
                  ^
        /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pymacconfig.h:43:17: note: previous definition is here
        #        define SIZEOF_SIZE_T           8
                        ^
        In file included from _mysql.c:44:
        /usr/local/mysql/include/my_config.h:423:9: warning: 'HAVE_WCSCOLL' macro redefined
        #define HAVE_WCSCOLL
                ^
        /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/pyconfig.h:891:9: note: previous definition is here
        #define HAVE_WCSCOLL 1
                ^
        _mysql.c:287:14: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                        cmd_argc = PySequence_Size(cmd_args);
                                 ~ ^~~~~~~~~~~~~~~~~~~~~~~~~
        _mysql.c:317:12: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                        groupc = PySequence_Size(groups);
                               ~ ^~~~~~~~~~~~~~~~~~~~~~~
        _mysql.c:470:14: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                                int j, n2=PySequence_Size(fun);
                                       ~~ ^~~~~~~~~~~~~~~~~~~~
        _mysql.c:1105:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                        len = mysql_real_escape_string(&(self->connection), out, in, size);
                            ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        _mysql.c:1107:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                        len = mysql_escape_string(out, in, size);
                            ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        _mysql.c:1146:9: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                size = PyString_GET_SIZE(s);
                     ~ ^~~~~~~~~~~~~~~~~~~~
        /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/stringobject.h:92:32: note: expanded from macro 'PyString_GET_SIZE'
        #define PyString_GET_SIZE(op)  Py_SIZE(op)
                                       ^
        /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/object.h:116:56: note: expanded from macro 'Py_SIZE'
        #define Py_SIZE(ob)             (((PyVarObject*)(ob))->ob_size)
                                         ~~~~~~~~~~~~~~~~~~~~~~^~~~~~~
        _mysql.c:1156:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                        len = mysql_real_escape_string(&(self->connection), out+1, in, size);
                            ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        _mysql.c:1158:9: warning: implicit conversion loses integer precision: 'unsigned long' to 'int' [-Wshorten-64-to-32]
                        len = mysql_escape_string(out+1, in, size);
                            ~ ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        _mysql.c:1252:11: warning: implicit conversion loses integer precision: 'Py_ssize_t' (aka 'long') to 'int' [-Wshorten-64-to-32]
                if ((n = PyObject_Length(o)) == -1) goto error;
                       ~ ^~~~~~~~~~~~~~~~~~
        /System/Library/Frameworks/Python.framework/Versions/2.7/include/python2.7/abstract.h:434:25: note: expanded from macro 'PyObject_Length'
        #define PyObject_Length PyObject_Size
                                ^
        _mysql.c:1444:10: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]
                                len = strlen(buf);
                                    ~ ^~~~~~~~~~~
        _mysql.c:1446:10: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]
                                len = strlen(buf);
                                    ~ ^~~~~~~~~~~
        _mysql.c:1482:11: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]
                                        len = strlen(buf);
                                            ~ ^~~~~~~~~~~
        _mysql.c:1484:11: warning: implicit conversion loses integer precision: 'size_t' (aka 'unsigned long') to 'int' [-Wshorten-64-to-32]
                                        len = strlen(buf);
                                            ~ ^~~~~~~~~~~
        _mysql.c:1567:10: warning: comparison of unsigned expression < 0 is always false [-Wtautological-compare]
                if (how < 0 || how >= sizeof(row_converters)) {
                    ~~~ ^ ~
        16 warnings generated.
        clang -bundle -undefined dynamic_lookup -Wl,-F. build/temp.macosx-10.8-intel-2.7/_mysql.o -L/usr/local/mysql/lib -lmysqlclient_r -lpthread -o build/lib.macosx-10.8-intel-2.7/_mysql.so -arch x86_64

    Successfully installed mysql-python
    Cleaning up…

But it still won't import:

    Phoebe-Brights-iMac:.python-eggs phoebebr$ python
    Python 2.7.2 (default, Jun 20 2012, 16:23:33) 
    [GCC 4.2.1 Compatible Apple Clang 4.0 (tags/Apple/clang-418.0.60)] 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 "/Library/Python/2.7/site-packages/MySQLdb/__init__.py", line 19, in <module>
        import _mysql
    ImportError: dlopen(/Library/Python/2.7/site-packages/_mysql.so, 2): Library not loaded: libmysqlclient.18.dylib
      Referenced from: /Library/Python/2.7/site-packages/_mysql.so
      Reason: image not found

And yet the file it is trying to load is there!

    Phoebe-Brights-iMac:/ phoebebr$ cd  /Library/Python/2.7/site-packages
    Phoebe-Brights-iMac:site-packages phoebebr$ ls
    total 792
    drwxr-xr-x  4 root  wheel     136 25 Oct 09:31 Django-1.4.2-py2.7.egg
    drwxr-xr-x  2 root  wheel     238 23 Nov 11:16 MySQL_python-1.2.4c1-py2.7.egg-info
    drwxr-xr-x  3 root  wheel     510 23 Nov 11:16 MySQLdb
    -rw-r--r--  1 root  wheel     119 11 Oct 23:59 README
    -rwxr-xr-x  1 root  wheel   67240 23 Nov 11:16 _mysql.so
    -rw-r--r--  1 root  wheel    2352 23 Nov 11:16 _mysql_exceptions.py
    -rw-r--r--  1 root  wheel    4243 23 Nov 11:16 _mysql_exceptions.pyc
    -rw-r--r--  1 root  wheel     306 23 Nov 10:37 easy-install.pth
    drwxr-xr-x  4 root  wheel     136 23 Nov 10:37 pip-1.2.1-py2.7.egg
    -rw-r--r--  1 root  wheel  295372 22 Oct 20:21 pymongo-2.3-py2.7-macosx-10.8-intel.egg
    -rw-r--r--  1 root  wheel   17994 22 Oct 20:22 sleepy.mongoose-0.1-py2.7.egg

Solution

  • In the past, I've always run into the libmysqlclient.18.dylib error on fresh installs.

    This usually does the trick for me.

    sudo ln -s /usr/local/mysql/lib/libmysqlclient.18.dylib /usr/lib/libmysqlclient.18.dylib
    

    I just had to do a clean install of Mountain Lion, and am having new issues with this package now. v1.2.4 is really new. You may try pip install MySQL-python==1.2.3 just to be safe as 1.2.4 is still RC1 I think.

    Edit:

    I'm a tard and installed 32bit MySQL on accident. 1.2.4c1 does indeed work. And as I would recommend using the export DYLD_LIBRARY_PATH=$DYLD_LIBRARY_PATH:/usr/local/mysql/lib/ instead of doing the symlink.