Recently, i worked a lot using Python on creating a web application and web api.

I use Django Web Framework and use MySQL for data storage.

To use mysql in django we need to install a connector driver, i use python module called MySQL-Python.

I’m using Python 2.7.x and OSx Sierra. And MySQL-Python depends on mysql-connector-c in OSx. Installing the connector is easier with brew.

brew install mysql-connector-c

Like always, i use pip to install dependency of my application. And then i run the following command to install MySQL-Python.

pip install MySQL-Python

Unfortunately, the installation failed and cause an error such:

IndexError: string index out of range

Then i check on google on how to solve the issue, and i found that the mysql_config hasn’t load the mysql configuration (yet). I don’t know if problem only exist on the latest mysql-connector-c package or the OSx version. Because i’ve never encounter the same error before.

To resolve the issue, we need to load or add mysql libs on mysql_config command

$ # editing mysql_config
$ vi `which mysql_config`
$ # or
$ vi /usr/local/bin/mysql_config

On the line # Create Options, we just need to add new line with:

libs="$libs -lmysqlclient -lssl -lcrypto"

To make sure the mysqlclient has been loaded, we can try to reinstall the MySQL-Python module using pip.

ref: https://github.com/PyMySQL/mysqlclient-python/issues/169