Import mysql.connector Python

I have two files. The first has a connection and receive 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 separately, it works and returns data.

I have another file called tasks.py where I am going to import this file, however this does not work! When I import the file, it gives me an error:

 ImportError: No module named mysql.connector 

What am I doing wrong?

+33
python mysql
source share
11 answers

Depending on the version of Python and how you installed it, it is possible that the MySQL connector is not installed, you can install it using pip

To install the MySQL connector:

 pip install mysql-connector-python 
+10
source share

I ran into a similar problem. My env details - Python 2.7.11 clause 9.0.1 CentOS version 5.11 (Final)

Error in 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 find an 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 mysql-connector-python-rf -

 $ pip install mysql-connector-python-rf 

Check

 $ 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 >>> 
+84
source share

This worked in ubuntu 16.04 for python 2.7:

 sudo pip install mysql-connector 
+15
source share

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

pip install mysql-connector-python-rf

+11
source share

use the command below

 python -m pip install mysql-connector 
+9
source share

In my case, I already installed the package

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

This gives me the same error, so I remove both packages with

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

and then install only the second package again

 pip install mysql-connector-python 

This solution worked for me.

+3
source share

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

+1
source share

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

+1
source share

If you have this error on PYCHARM: ImportError: There is no module named mysql.connector

Try this solution: Open Pycharm, go to File-> Settings-> Project-> Python Interpreter inside Pycharm, then click the + icon to install the mysql connector. The problem is solved!

+1
source share

For me, downloading the .deb file and installing it ( dpkg -i mysql-connector-python_2.1.7-1ubuntu16.04_all.deb ) dpkg -i mysql-connector-python_2.1.7-1ubuntu16.04_all.deb . The Python download is here (you will need to first create a free MySQL login to download). Make sure you select the correct version, i.e. ( python_2.1.7 vs. python-py3_2.1.7 ). For me, only the version of "Independent Independent" worked.

0
source share

I ran into the same problem on WAMP. Find the connectors available with the pip command. You can run this from any prompt if the Python ENV variables are set correctly.

  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 the MySQL connector

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

check 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.

0
source share

All Articles