Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
138 views
in Technique[技术] by (71.8m points)

python - ImportError: No module named mysql.connector using Python2

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

1.4m articles

1.4m replys

5 comments

56.9k users

...