Solving error when running pip install MySQL-python – IndexError: string index out of range ( python mysql error )

Error when running pip install MySQL-python – IndexError: string index out of range ( python mysql error ) is an error which occurs because of a faulty MySQL-python installation.

I will explain why this error is taking place and how to fix it, while also trying to add other solutions that could help us solve the problem.

Exploring : error when running pip install MySQL-python – IndexError: string index out of range ( python mysql error )

This is an error which occurs because of a faulty MySQL-python installation.

You should avoid mixing between different errors. The error message should look like the error message bellow.

                                                                       #
IndexError: string index out of range
                                                                       #

Bellow, we will take care of the error using multiple possible methods.

Solution 1 : Choose the correct mysql_config before installing mysqlclient and after removing mysql-connector-c

The first method is a step by step method, please do not miss any steps.

First, we should remove mysql-connector-c

                                                                       #
brew uninstall mysql-connector-c
                                                                       #

Then install mysql using brew

                                                                       #
brew install mysql@5.7
                                                                       #

Open the file mysql_config , located in the following location /usr/local/bin/mysql_config

Replace Libs = “$libs -l” with

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

Then, run the following command

                                                                       #
brew link --force mysql@5.7
                                                                       #

Then use the export LDFLAGS command

                                                                       #
export LDFLAGS=-L/usr/local/opt/openssl/lib
                                                                       #

Finally, install mysqlclient

                                                                       #
pip install mysqlclient
                                                                       #

That is it, the error should be gone. Congrats if this has worked for you.

Please try the method bellow if this one did not work.

Solution 2 : install libmysqlclient-dev and install MySql-python

This method has two steps, the first step is to install libmysqlclient-dev

You can install libmysqlclient-dev using the command bellow

                                                                       #
sudo apt install libmysqlclient-dev
                                                                       #

The second step is to install MySql-python, you can use the pip package manager for that

                                                                       #
pip install MySql-python
                                                                       #

The two commands above will probably fix your issue.

I hope this guide solved your problem, thank you for reading this entire blog post..

Summing-up : 

This is the end of this article, I hope we helped you solve the error or at least show you why it happens and guided you in the right direction. If you want to help, you can donate to our Kofi account.

Thank you for reading, keep coding and cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/