Fixing python mysql import error – ImportError: No module named ‘MySQL’

python mysql import error – ImportError: No module named ‘MySQL’ is an error which occurs when you import mysql.connector without installing it or when the installation of mysql.connector is faulty.

In this article I am going to explain why the error is occurring and how to solve it using multiple solutions which may work for your particular case.

Exploring the Error : python mysql import error – ImportError: No module named ‘MySQL’

This is an error which occurs when you import mysql.connector without installing it or when the installation of mysql.connector is faulty.

The error message should look like the message bellow. Make sure you are not dealing with another error.

                                                                       #
import mysql.connector
ImportError: No module named 'mysql'
                                                                       #

Bellow is a number of tested solutions that I have tried and have worked for me.

Solution 1 : rename your mysql.py file if you made this mistake

This is only for people who picked have picked mysql.py as a name for their file.

You do not want python to confuse between modules and files, use your own unique names for your files and avoid any conflict with package and module names. Do not pick names like python.py pip.py matplotlib.py or mysql.py … etc. I think you get the idea.

If this is not the problem here then skip to the detailed solution bellow.

Solution 2 : install mysql-connector and/or install mysql-connector-python-rf

The error might occur when you forget to install mysql-connector or when you have a faulty installation.

You can install mysql-connector in many ways depending on your environment. You can try

                                                                       #
python3 -m pip install mysql-connector
                                                                       #

or try this command instead

                                                                       #
pip install mysql-connector # try this if you have python 3 : pip3 install mysql-connector
                                                                       #

if you have conda, try this command

                                                                       #
conda install -c anaconda mysql-connector-python
                                                                       #

If none of the above has worked please try installing mysql-connector-python-rf using the command bellow

                                                                       #
pip install mysql-connector-python-rf
                                                                       #

Your error should be gone. I apologize if this article did not help you solve the error but I believe I covered most of the solutions which have the biggest probability of solving your issue, thank you for reading and good luck.

Summing-up

That’s it fellow developers, this is the end of this guide, I hope you found this useful in solving your particular issue, if you have the means and want to help, please support our work on our Kofi account, you do not have to, but you can donate if you want.

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