Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python | MySQL | AttributeError: module 'mysql.connector' has no attribute 'connect'

Tags:

python

mysql

I am learning a new library mysql in python. I have tried executing the below command,

import mysql.connector

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

and got the below error!

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'

I looked into similar other Stack overflow questions... Didn't get a solution. I tried renaming my file too, that didn't help. Please help on the same!!!

like image 612
Viknesh S K Avatar asked Oct 25 '25 01:10

Viknesh S K


2 Answers

When I had the same problem. I had to just uninstall the package with pip uninstall mysql-connector-python and reinstall it with pip install mysql-connector-python and it worked :D

like image 179
Ang Avatar answered Oct 26 '25 15:10

Ang


This generally happens when you install the wrong package. Probably during installation, you have used the following pip command: pip install mysql-connector

But you are required to install the package: pip install mysql-connector-python

Like this:

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)
like image 24
Tuhin Mitra Avatar answered Oct 26 '25 17:10

Tuhin Mitra



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!