Solving Python MySQLdb Error – No module named ‘MySQLdb’. Did you install mysqlclient or MySQL-python?

MySQLdb Error – No module named ‘MySQLdb’. Did you install mysqlclient or MySQL-python? is an error which happens when your mysqlclient installation or installation process is faulty.

In this article I am going to explain what happens when you get this error and how you can solve it with a main solution, we will also explore other solutions which can possibly solve the issue.

Exploring MySQLdb Error – No module named ‘MySQLdb’. Did you install mysqlclient or MySQL-python?

MySQLdb Error – No module named ‘MySQLdb’. Did you install mysqlclient or MySQL-python? is an error which happens when your mysqlclient installation or installation process is faulty.

The error message should be the exact as this one or at least very similar in order to avoid all types of confusion.

                                                                       #
File "C:\Users\user\env\lib\site-packages\django\db\backends\mysql\base.py", line 30, in <module>
    'Did you install mysqlclient or MySQL-python?' % e
django.core.exceptions.ImproperlyConfigured: Error loading MySQLdb module: No module named 'MySQLdb'.
Did you install mysqlclient or MySQL-python?
                                                                       #

Bellow you can find the solution which worked for me plus other popular possible solutions among developers who already faced this error.

Solution 1 : install pymysql this way

The solution is simple, you can use pip to install pymysql and for pip3 you can use python3

                                                                       #
pip install pymysql            # pip for python  
python3 -m pip install PyMySQL # pip3 for python3
                                                                       #

Finally, you should edit settings.py and init.py in your directory by adding.

                                                                       #
import pymysql
pymysql.install_as_MySQLdb()
                                                                       #

The solution should have solved the issue and the error should be gone. If that is not the case, please consider using or trying the solution bellow.

Solution 2 : re-install mysqlclient.

The second solution is for people who have installed mysqlclient already and are still getting the error.

Use the commands bellow to re-install mysqlclient.

                                                                       #
pip uninstall mysqlclient
export LDFLAGS="-L/usr/local/opt/openssl/lib" and export CPPFLAGS="-I/usr/local/opt/openssl/include" as explained here
pip install mysqlclient
                                                                       #

If the solutions above helped you, consider supporting us on Kofi, any help is appreciated.

Summing-up

Thank you guys for reading, I hope this article helped you solve the issue at hand, I hope the error is gone after following one of the solutions I described above.

Keep coding, errors are normal, keep learning, cheers. If you want to learn more about Python, please check out the Python Documentation : https://docs.python.org/3/