Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ImportError: No module named 'MySQL'

Tags:

python

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.

like image 885
Melanie Avatar asked Oct 01 '15 00:10

Melanie


People also ask

How to fix error of No module named mysql?

The Python "ModuleNotFoundError: No module named 'mysql'" occurs when we forget to install the mysql-connector-python module before importing it or install it in an incorrect environment. To solve the error, install the module by running the pip install mysql-connector-python command.

How do I import mysql connector to Spyder?

First, run the pip install mysql-connector command in Spyder IPython Console(Where executions are done) and when execution is finished then restart kernel after that you can use import mysql. connector .


2 Answers

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

like image 98
Rishi Avatar answered Oct 07 '22 18:10

Rishi


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

like image 29
ilexcel Avatar answered Oct 07 '22 19:10

ilexcel