Python | MySQL | AttributeError:模块“ mysql.connector”没有属性“ connect”

时间:2019-12-04 12:50:32

标签: python mysql

我正在用python学习一个新的库mysql。我尝试执行以下命令,

import mysql.connector

mydb= mysql.connector.connect(
            host= 'localhost',
            user= 'root',
            passwd= 'skviknesh'
            )
print(mydb)

并出现以下错误!

Traceback (most recent call last):

  File "<ipython-input-1-f9b9bc67dd68>", line 3, in <module>
    mydb= mysql.connector.connect(

AttributeError: module 'mysql.connector' has no attribute 'connect'

我研究了其他类似的Stack溢出问题...没有解决方案。 我也尝试过重命名文件,但没有帮助。 请帮忙!

2 个答案:

答案 0 :(得分:0)

通常会在安装错误的软件包时发生。 可能在安装期间,您使用了以下pip命令: pip install mysql-connector

但是您还需要安装该软件包: pip install mysql-connect-python

赞:

C:\Users\tuhin Mitra>pip install mysql-connector-python
Requirement already satisfied: mysql-connector-python in c:\users\tuhin mitra\appdata\local\programs\python\python37-32\lib\site-packages (8.0.19)
Requirement already satisfied: protobuf==3.6.1 in c:\users\tuhin mitra\appdata\local\programs\python\python37-32\lib\site-packages (from mysql-connector-python) (3.6.1)
Requirement already satisfied: dnspython==1.16.0 in c:\users\tuhin mitra\appdata\local\programs\python\python37-32\lib\site-packages (from mysql-connector-python) (1.16.0)
Requirement already satisfied: setuptools in c:\users\tuhin mitra\appdata\local\programs\python\python37-32\lib\site-packages (from protobuf==3.6.1->mysql-connector-python) (45.1.0)
Requirement already satisfied: six>=1.9 in c:\users\tuhin mitra\appdata\roaming\python\python37\site-packages (from protobuf==3.6.1->mysql-connector-python) (1.12.0)

答案 1 :(得分:0)

当我遇到同样的问题时。我只需要用 pip uninstall mysql-connector-python 卸载软件包,然后用 pip install mysql-connector-python 重新安装它,它就起作用了:D