已经安装了mysql-connector和mysql-connector-python,使用python连接数据库,导入import mysql.connector仍报错:
import mysql.connector
# Connect to server
cnx = mysql.connector.connect(
host="127.0.0.1",
port=3306,
user="alice",
password="dogsandcats123")
# Get a cursor
cur = cnx.cursor()
# Execute a query
cur.execute("SELECT CURDATE()")
# Fetch one result
row = cur.fetchone()
print("Current date is: {0}".format(row[0]))
# Close connection
cnx.close()
控制台报错ModuleNotFoundError: No module named 'mysql'
:
C:\ProgramFiles\anaconda3\python.exe C:\Users\pengkangzhen\PycharmProjects\wk-projects\facility-operation-path-opt\main.py
Traceback (most recent call last):
File "C:\Users\pengkangzhen\PycharmProjects\wk-projects\facility-operation-path-opt\main.py", line 3, in <module>
import mysql.connector
ModuleNotFoundError: No module named 'mysql'
conda list查看当前环境的确已安装了mysql-connector和mysql-connector-python
有人说mysql-connector已弃用,应该使用mysql-connector-python(https://blog.csdn.net/qq_45463933/article/details/121619416)
很奇怪,查找python interperter发现我的py3.9_or环境并不在envs目录下,选择py3.9_or创建py3.9_or(2)环境在envs目录下,按理说这两个路径是一样的,将Anaconda卸载。
重装Anaconda后,执行安装成功
pip install mysql-connector-python