gpt4 book ai didi

Python - 属性错误 : module 'mysql' has no attribute 'connector'

转载 作者:搜寻专家 更新时间:2023-10-30 22:28:20 25 4
gpt4 key购买 nike

我是 python 的新手,我正在使用 python 3.6 和来自 mysql 网站的 mysql 连接器

pip install --allow-external mysql-connector-python mysql-connector-python

一切顺利。我尝试了 3 个来自 mysql 网站的例子创建数据库和表,插入,选择。

但是在第二个例子之后它停止工作并给出错误

Traceback (most recent call last):
File "select.py", line 3, in <module>
import mysql.connector
File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\site-packages\
mysql\connector\__init__.py", line 37, in <module>
from .connection import MySQLConnection
File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\site-packages\
mysql\connector\connection.py", line 45, in <module>
from .network import MySQLUnixSocket, MySQLTCPSocket
File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\site-packages\
mysql\connector\network.py", line 28, in <module>
import socket
File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\socket.py", li
ne 52, in <module>
import os, sys, io, selectors
File "C:\Users\preet\AppData\Local\Programs\Python\Python36\lib\selectors.py",
line 11, in <module>
import select
File "C:\Users\preet\Desktop\ptyhon-newspaper\select.py", line 7, in <module>
cnx = mysql.connector.connect(user='root', password='Jinqm21k',
AttributeError: module 'mysql' has no attribute 'connector'

示例代码

from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector



cnx = mysql.connector.connect(user='root', password='****',
host='localhost',
database='worpress')
cursor = cnx.cursor()

query = ("SELECT first_name, last_name, hire_date FROM employees "
"WHERE hire_date BETWEEN %s AND %s")

hire_start = datetime.date(1999, 1, 1)
hire_end = datetime.date(1999, 12, 31)

cursor.execute(query, (hire_start, hire_end))

for (first_name, last_name, hire_date) in cursor:
print("{}, {} was hired on {:%d %b %Y}".format(
last_name, first_name, hire_date))


cursor.close()
cnx.close()

所有示例停止工作,包括创建、插入、选择

我无法弄清楚它有什么问题

最佳答案

你的问题是 mysql 库试图导入一些包,这些包导入了被你的模块“select.py”隐藏的“select”包。重命名“select.py”后,请提供更新后的回溯,现在无法正常工作。

我还建议您不要在 .py 文件本身中编写代码,除非您希望此代码在模块导入时执行。相反,创建一个例程并在 __main__ 部分调用它。

from __future__ import print_function
from datetime import date, datetime, timedelta
import mysql.connector

def print_employees():
cnx = mysql.connector.connect(user='root', password='Jinqm21k',
host='localhost',
database='worpress')
cursor = cnx.cursor()
...

if __name__ = '__main__':
print_employees()

关于Python - 属性错误 : module 'mysql' has no attribute 'connector' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/48134780/

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