gpt4 book ai didi

python - ImportError : No module named mysql. 使用 Python2 的连接器

转载 作者:IT老高 更新时间:2023-10-28 22:09:40 26 4
gpt4 key购买 nike

我有两个文件。第一个有连接和数据的获取。我导入mysql.connector。这个文件叫做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

当我单独运行这个文件时,它可以工作并返回数据。

我有另一个名为 tasks.py 的文件,我将在其中导入此文件,但是,这不起作用!当我导入文件时,它给了我错误:

ImportError: No module named mysql.connector

我做错了什么?

最佳答案

我遇到了类似的问题。我的环境细节 - python 2.7.11点 9.0.1CentOS 5.11 版(最终版)

python 解释器出错 -

>>> import mysql.connector
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ImportError: No module named mysql.connector
>>>

使用 pip 搜索可用模块 -

 $ 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

安装mysql-connector-python-rf -

$ pip install mysql-connector-python-rf

验证

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

关于python - ImportError : No module named mysql. 使用 Python2 的连接器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24272223/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com