Viknesh S K
Viknesh S K

Reputation: 121

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

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!!!

Upvotes: 4

Views: 13592

Answers (3)

Programmer
Programmer

Reputation: 75

I was also facing the same issue while i am trying to connect to mysql from python, i have corrected below issue, you may have one of the issue below

  1. use approriate library installation
  2. use exact password type to connect with database(because by default mysql use caching_sha2_password). you have to pass the password type explisit.

Upvotes: 0

Tuhin Mitra
Tuhin Mitra

Reputation: 727

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)

Upvotes: 1

Ang
Ang

Reputation: 111

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

Upvotes: 11

Related Questions