Question

[Solved] ImportError: No module named ‘MySQL’

I have downloaded the Connector/Python for MySQL successfully. I used the following code in Python’s shell to test my connection:

import mysql.connector

I received the following error message:

Traceback (most recent call last):
  File "<pyshell#8>", line 1, in <module>
    import mysql.connector
ImportError: No module named 'mysql'

I can’t figure out why MySQL is not being recognized.

Enquirer: Melanie

||

Solution #1:

I was facing the similar issue. My env details –
Python 2.7.11
pip 9.0.1
CentOS release 5.11 (Final)

Error on python interpreter –

>>> import mysql.connector
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>

Use pip to search the available module –

$ pip search mysql-connector | grep --color mysql-connector-python



mysql-connector-python-rf (2.2.2)        - MySQL driver written in Python
mysql-connector-python (2.0.4)           - MySQL driver written in Python

Install the mysql-connector-python-rf –

$ pip install mysql-connector-python-rf

Verify

$ python
Python 2.7.11 (default, Apr 26 2016, 13:18:56)
[GCC 4.1.2 20080704 (Red Hat 4.1.2-54)] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> import mysql.connector
>>>

Thanks =)

For python3 and later use the next command:
$ pip3 install mysql-connector-python-rf

Respondent: Rishi

Solution #2:

Use
pip3 install mysql-connector
to install the python packaged (if you are using Python 3. For Python 2 you can use pip).

Respondent: ilexcel

Solution #3:

May be simple install from cli?

pip3 install mysql-connector-python-rf

Package name differs from import library name

Or my universal variant in code:

import pip
pip.main(['install','mysql-connector-python-rf'])

For new version of pip:

from pip._internal import main
main(['install','mysql-connector-python-rf'])

It’s better – install needed modules in running python installation (if many)

Respondent: ??????? ?????????

Solution #4:

The silly mistake I had done was keeping mysql.py in same dir. Try renaming mysql.py to another name so python don’t consider that as module.

Respondent: Deepak Chauhan

Solution #5:

I tried all the answers but not worked for me.
It is a python version problem, in the end, I realized that python 3 scripts need explicit pip command for python 3, at least on ubuntu 18.

python3 -m pip install mysql-connector
Respondent: Mohannd

Solution #6:

Try that out bud

sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz

gunzip mysql-connector-python-2.1.3.tar.gz

tar xf mysql-connector-python-2.1.3.tar

cd mysql-connector-python-2.1.3

sudo python3 setup.py install
Respondent: gdxn96

Solution #7:

just a note, I just installed mysql on an ubuntu 16.04 server. I tried different options in the following order:

  • using the package from repository sudo apt-get install python-mysqldb: Was installed correctly, but unfortunatelly python returned ImportError: No module named 'mysql'
  • using the ubuntu package from Mysql: wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python_2.1.4-1ubuntu16.04_all.deb. Installed correctly with sudo dpckg -i package_name, but python returned the same error.
  • using tar.gz file, installing with python, worked ok.
    • wget http://dev.mysql.com/get/Downloads/Connector-Python/mysql-connector-python-2.1.6.tar.gz (used 2.1.4 at that time)
    • tar -xf mysql-connector-python-2.1.6.tar.gz
    • cd mysql-connector-python-2.1.6/
    • sudo python3 setup.py install

Did not investigate why the first two failed, might try later.
Hope this helps someone.

Respondent: nihilis

Solution #8:

You need to use anaconda to manage python environment dependencies. MySQL connector can be installed using conda installer

conda install -c anaconda mysql-connector-python
Respondent: akshat thakar

Solution #9:

I found that @gdxn96 solution worked for me, but with 1 change.

sudo wget http://cdn.mysql.com//Downloads/Connector-Python/mysql-connector-python-2.1.3.tar.gz
tar -zxvf mysql-connector-python-2.1.3.tar
cd mysql-connector-python-2.1.3
sudo python3 setup.py install
Respondent: Spac3

Solution #10:

sudo python3 -m pip install mysql-connector-python
Respondent: NemboKid

Solution #11:

I have found another reason:
If your program file’s path contains space or special characters, then you’d get this error.

Respondent: Gábor Soós

Solution #12:

This problem was a plague to me!!! The 100% solution is to forget using the mysql module: import mysql.connector, instead use pymysql via import pymysql. I installed it via the instructions: python3 -m pip install PyMySQL

made a change to the:
1. Import statement
2. The connector
3. The cursor

After that everything worked like a charm. Hope this helps!

Respondent: Mr. B7

Solution #13:

You need to use one of the following commands. Which one depends on different-2 OS and software you have and use.

sudo easy_install mysql-python (mix os)
sudo pip install mysql-python (mix os)
sudo apt-get install python-mysqldb (Linux Ubuntu, ...)
cd /usr/ports/databases/py-MySQLdb && make install clean (FreeBSD)
yum install MySQL-python (Linux Fedora, CentOS ...)
Respondent: Ashish Gupta

Solution #14:

I just moved source folder connector from folder mysql to site-packages.
And run import connector

Respondent: ???????

Solution #15:

For 64-bit windows

install using wheel

pip install wheel download from http://www.lfd.uci.edu/~gohlke/pythonlibs/#mysql-python

For python 3.x:

pip install mysqlclient-xxxxxxxxx-win_amd64.whl

For python 2.7:

pip install mysqlclient-xxxxxxxxx-win_amd64.whl
Respondent: Ashish Gupta

Solution #16:

My project was using pipenv. The following command worked for me:

pipenv install mysql-connector-python
Respondent: Haris ur Rehman

Solution #17:

run

pip list 

to see list of packages you have installed. If it has
mysql-connector-python then that is fine.

Remember not to name your python script file as mysql.py

Respondent: ytdm

Solution #18:

I did try re-install with apt and pip.
But the only thing that worked was install from source.

On Ubuntu 18.04 x64

Respondent: DeDenker

The answers/resolutions are collected from stackoverflow, are licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0 .

Most Popular

To Top
India and Pakistan’s steroid-soaked rhetoric over Kashmir will come back to haunt them both clenbuterol australia bossier man pleads guilty for leadership role in anabolic steriod distribution conspiracy