0

Possible Duplicate:
How to install MySQLdb (Python data access library to MySQL) on Mac OS X?

Although this question has been asked before here, the question and subsequent answers that are posted there are relevant to OS X (Snow Leopard) and not to OS X Lion and OS X Mountain Lion. There were patches for Python and MySQL that shipped in OS X Lion and OS X Mountain Lion that impact the process of installing the Python-MySQLdb-connector smoothly on Mac OS X 10.7 (Lion) and Mac OS X 10.8 (Mountain Lion). This is is the only reason this question is being repeated. Please feel free to refer to the old question in case you are running an older version of OS X.

I am running OS X 10.8.2 (Mountain Lion) and have python 2.7.2 installed -

==> 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.

But when I run import MySQLdb on python, it fails with the following message -

==> 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>
ImportError: No module named MySQLdb

Any input would be helpful.

Community
  • 1
  • 1
Arunabh Das
  • 13,212
  • 21
  • 86
  • 109

2 Answers2

6

I solved the above issue by following the steps below :

The steps below work for Mountain Lion (OS X 10.8.2) -

Setup MySQL-python package

Download the latest MySQL (64-bit, tar archive) here

  • Extract tar contents
  • Rename folder to “mysql”
  • Copy folder to /usr/local/
  • You should now have mysql installed on /usr/local/mysql

Download the latest MySQL-python package here

Type on the Terminal:

tar -xvzf MySQL-python-1.2.3.tar.gz cd MySQL-python-1.2.3

Edit site.cfg on your favorite text editor and change the mysql path as below: mysql_config = /usr/local/mysql/bin/mysql_config

You are now ready to build and install MySQL-python:

sudo python setup.py build sudo python setup.py install

Edit your ~/.bash_profile, add the following line:

export DYLD_LIBRARY_PATH=/usr/local/mysql/lib/

If you are already using MAMP you still have to follow the above MySQL installation process, as MAMP by default doesn’t include the required header files to build MySQL-python. Moreover if you prefer MAMP, you don’t have to run mysql from /usr/local/mysql. On your settings.py remember to set “127.0.0.1″ as your preferred db host in case that “localhost” doesn’t work.

Test MySQL-python installation

Restart Terminal and type:

python import MySQLdb

The other thing I noticed is that it helps to restart the mac after installing mysql.

The steps posted on the following link work for OS X 10.6.8 -

How to install MySQLdb (Python data access library to MySQL) on Mac OS X?

Community
  • 1
  • 1
Arunabh Das
  • 13,212
  • 21
  • 86
  • 109
0

mysql-connector-python from offical mysql(Oracle) is a good choice for both python 2.x and 3.x.

It's a pure python library without mysql client.

imxylz
  • 7,847
  • 4
  • 28
  • 25