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 usersWhen 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.connectorWhat am I doing wrong?
15 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 PythonInstall the mysql-connector-python-rf -
$ pip install mysql-connector-python-rfVerify
$ 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
>>> 3 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 This worked in ubuntu 16.04 for python 2.7:
sudo pip install mysql-connector 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.connectorI solved the problem by changing the file name.
4use the command below
python -m pip install mysql-connector 0 I used the following command to install python mysql-connector in Mac. it works
2pip install mysql-connector-python-rf
In my case i already installed the package
pip install mysql-connector
pip install mysql-connector-pythonIt giving me the same error so, i uninstall the both package by using
pip uninstall mysql-connector
pip uninstall mysql-connector-pythonand then again install the second package only
pip install mysql-connector-pythonThis 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!
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.
1I use Python 3.4 for windows and installed mysql library from
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 connectionRun the following command to install mysql-connector
C:\Users\Admin>pip install mysql-connectorverify 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.
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.
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 foundBut I resolved it with this:
pip install mysql-connector-python 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.connectorIn 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-pythonor something like this
dnf install mysql-connector-python3.noarchThen 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)).