iammehrabalam
iammehrabalam

Reputation: 1325

PyMySQL error: "AttributeError: 'module' object has no attribute 'connect'"

Getting connection error in PyMySQL:

Error

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='pymysql')
AttributeError: 'module' object has no attribute 'connect'

code

import pymysql

conn = pymysql.connect(host='127.0.0.1', port=3306, user='root', passwd='', db='pymysql')

cur = conn.cursor()

cur.execute("SELECT Host,User FROM user")

print(cur.description)

print()

for row in cur:
   print(row)

cur.close()
conn.close()

Upvotes: 0

Views: 9998

Answers (5)

Bart Kedryna
Bart Kedryna

Reputation: 11

In my case the problem was permissions - they were 700 and owned by root. This command fixed the problem:

sudo chmod -R 755 /usr/local/lib/python3.9/site-packages/pymysql

Upvotes: 0

rogexis
rogexis

Reputation: 1

I had that error, due I named my py file as select.py. I don't know how you named it but you could try changing the name file.

Upvotes: 0

Kalpataru sahoo
Kalpataru sahoo

Reputation: 7

The connection was successful in this code:

con=pymysql.connect('localhost','root','root','mydb27')

But now I am following this code:

connection = pymysql.connect(host='localhost',
                             user='root',
                             password='kanha@12345',
                             database='mydb23',
                             charset='utf8mb4')
cur1=connection.cursor()
cur1.execute("select * from emp where city='hyd'")

Upvotes: 0

nsjethani
nsjethani

Reputation: 119

Use capital 'C' in pymysql.Connect.

conn = pymysql.Connect(host='127.0.0.1', port=3306, user='root', passwd='', db='pymysql')

The above statement should work. It worked for me!

Upvotes: 3

Ignacio Vazquez-Abrams
Ignacio Vazquez-Abrams

Reputation: 798446

You've called some other module "pymysql". Look for a file named "pymysql.py" and rename it, and remove any associated .pyc file.

Upvotes: 1

Related Questions