python中import mysql.connector出错无模块,且是已经pip install mysql-connector情况下

2023-12-13 04:35:42

已经安装了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

在这里插入图片描述

文章来源:https://blog.csdn.net/qq_43276566/article/details/134862154
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。