ImportError: No module named mysql.connector using Python2
Question:
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?
Answers:
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
I use Python 3.4 for windows and installed mysql library from http://dev.mysql.com/downloads/connector/python/
I used the following command to install python mysql-connector in Mac. it works
pip install mysql-connector-python-rf
This worked in ubuntu 16.04 for python 2.7:
sudo pip install mysql-connector
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
>>>
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.
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.
use the command below
python -m pip install mysql-connector
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:UsersAdmin>pip install mysql-connector
verify the installation
C:UsersAdmin>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.
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.
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!
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
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.
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.
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)).
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?
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
I use Python 3.4 for windows and installed mysql library from http://dev.mysql.com/downloads/connector/python/
I used the following command to install python mysql-connector in Mac. it works
pip install mysql-connector-python-rf
This worked in ubuntu 16.04 for python 2.7:
sudo pip install mysql-connector
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
>>>
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.
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.
use the command below
python -m pip install mysql-connector
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:UsersAdmin>pip install mysql-connector
verify the installation
C:UsersAdmin>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.
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.
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!
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
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.
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.
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)).