64

I have two files. The first one has the connection and the getting of data. I import mysql.connector. This file is called tasksSql.py

def get_users():
    import mysql.connector

    con = mysql.connector.connect(user='****', password='*****',
                                  host='127.0.0.1',
                                  database='tasks')
    c = con.cursor()

    users = []    
    c.execute("""SELECT * FROM task_user""")

    for row in c:
        user = {
            'id': row[0],
            'first': row[1],
            'last': row[2],
            'email': row[3],
            'password': row[4],
            'creation_date': row[5]
        }
        users.append(user)
    c.close()
    return users

When I run this file singly it works and returns data.

I have another file named tasks.py where I am going to be importing this file, however, this isn't working! When I import the file, it gives me the error:

ImportError: No module named mysql.connector

What am I doing wrong?

15 Answers 15

96

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
>>>
Sign up to request clarification or add additional context in comments.

3 Comments

mysql-connector-python is updated to 8.0.11 as of now and the rf variant seems to be not updated anymore.
Why the -rf version rather than the plain one? What's the difference?
Thanks. Fixed my issue because of your answer.
75

Depending on your python version and how you installed it, it is possible that mysql connector isn't installed, you can install it with pip

To install mysql connector:

pip install mysql-connector-python

5 Comments

I did install it with pip, but it says that it's already installed
Are you using python 3.x? If you are, have you installed it with pip3.x command?
Did this several times and keeps saying no module installed, not sure what I'm doing wrong! it lets me install it over and over again and doesn't say it's already installed so am I missing something?
@MrJoshFisher I'm having the same issue, did you find a solution?
@MrJoshFisher I believe for python3 the install package is just mysql-connector
22

use the command below

python -m pip install mysql-connector 

Comments

21

This worked in ubuntu 16.04 for python 2.7:

sudo pip install mysql-connector

Comments

19

I had a file named mysql.py in the folder. That's why it gave an error because it tried to call it in the import process.

import mysql.connector

I solved the problem by changing the file name.

4 Comments

oh my god i had the same issue , great job
wow, I had the same issue. I never would've figured this out were it not for this post reply!
Same :facepalm: Thanks @ stackoverflow.com/users/11980583/hamdi-yilmaz :+1:
I had a file named connector.py in the folder. Changing that solved error
12

I used the following command to install python mysql-connector in Mac. it works

pip install mysql-connector-python-rf

2 Comments

I think you should start your answer from pointing to OP that they may have not installed the package; and after that propose your solution, which may need some adjustments, depending on the case.
It is also worked for me on ubuntu 16.04 for python 2.7
9

In my case i already installed the package

pip install mysql-connector
pip install mysql-connector-python

It giving me the same error so, i uninstall the both package by using

pip uninstall mysql-connector
pip uninstall mysql-connector-python

and then again install the second package only

pip install mysql-connector-python

This solution worked for me.

Comments

9

If you have this error on PYCHARM: ImportError: No module named mysql.connector

Try this solution: Open Pycharm go to File->Settings-> Project->Python Interpreter inside Pycharm, Then press + icon to install mysql-connector. Problem solved!

2 Comments

Thank you so much sir! I have been searching for this for 3 hours.
This answer deserves a "Green Tick".
3

pip install mysql-connector is deprecated and doesn't work as of 2022.

pip install mysql-connector-python is the correct command to use.

Also, the pip search command recommended in the accepted answer is deprecated as of 2022 (see error using pip search (pip search stopped working)).

screenshot of PyPI for mysql-connector

Comments

2

In my case, after the recent (Mac OS High Sierra) upgrade and the subsequent brew upgrade, I started to see the above error. I followed the above instructions but still got the same error message. Then I realised that I had to use python2 which points to the brew installed python rather than the os x installed one.

1 Comment

Did you solve the issue by using Python2? Please confirm if the issue is solved or still persists.
1

I use Python 3.4 for windows and installed mysql library from http://dev.mysql.com/downloads/connector/python/

Comments

1

I was facing the same issue on WAMP. Locate the connectors available with pip command. You can run this from any prompt if Python ENV variables are properly set.

    pip search mysql-connector

    mysql-connector (2.2.9)                           - MySQL driver written in Python
    bottle-mysql-connector (0.0.4)                    - MySQL integration for Bottle.
    mysql-connector-python (8.0.15)                   - MySQL driver written in Python
    mysql-connector-repackaged (0.3.1)                - MySQL driver written in Python
    mysql-connector-async-dd (2.0.2)                  - mysql async connection

Run the following command to install mysql-connector

    C:\Users\Admin>pip install mysql-connector

verify the installation

    C:\Users\Admin>python
    Python 3.7.3 (v3.7.3:ef4ec6ed12, Mar 25 2019, 22:22:05) [MSC v.1916 64 bit 
    (AMD64)] on win32
    Type "help", "copyright", "credits" or "license" for more information.
    >>> import mysql.connector
    >>>

This solution worked for me.

Comments

0

What worked for me was to download the .deb file directly and install it (dpkg -i mysql-connector-python_2.1.7-1ubuntu16.04_all.deb). Python downloads are located here (you will need to create a free MySQL login to download first). Make sure you choose the correct version, i.e. (python_2.1.7 vs. python-py3_2.1.7). Only the "Architecture Independent" version worked for me.

2 Comments

Downloading doesn't require you to sign in. Check the direct download link at the bottom
@harpratap I never noticed that. Thank you.
0

I had problem with my MySQL installation, I was given this error:

<module> import mysql.connector ModuleNotFoundError: No module named 'mysql' (zrealestate) 
[root@localhost zrealestate]# brew install mysql
-bash: brew: command not found

But I resolved it with this:

pip install mysql-connector-python

Comments

0

I have a similar problem and the solution was to install mysql-connector-python to the actual script environment. Check if your script has at the first line which interpreter should be used. It would be something like

#!/usr/bin/python3
import mysql.connector

In this case, install mysql-connector-python to the environment of the python interpreter. You should use pip or system package manager.

pip3 install mysql-connector-python

or something like this

dnf install mysql-connector-python3.noarch

Then test it by running a propper python interpreter.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.