Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm running Ubuntu 16.04, trying to connect to mysql in python:

    import mysql
    username = 'root'

    cnx = mysql.connector.connect(user=username, database='db')
    cnx.close()

But I get an error:

    File "pysql1.py", line 4, in <module>
      cnx = mysql.connector.connect(user=username, database='db')
    AttributeError: module 'mysql' has no attribute 'connector'

I installed the mysql python module by downloading the package here. I tried sudo apt-get install python-mysql.connector to no avail. Any pointers?

EDIT: after adding import mysql.connector I got an unrelated permissions error which I've now resolved, so that's what I needed ty lots!!!

like image 706
planpony69 Avatar asked Sep 30 '17 14:09

planpony69


4 Answers

The solution is to execute :

import mysql.connector # or from mysql import connector

Because the module connector is only available when you import it explicitly :

import mysql

print(dir(mysql))
>>> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', 
'__package__', '__path__', '__spec__']

import mysql.connector

print(dir(mysql))
>>> ['__builtins__', '__cached__', '__doc__', '__file__', '__loader__', '__name__', 
'__package__', '__path__', '__spec__', 'connector']

The __init__ file in the module mysql doesn't import the module connector.

mysql
|_______ __init__.py # no import at this level
|_______ connector
         |________ __init__.py

This could work implicitly if connector was imported inside __init__ with : from . import connector.

like image 155
PRMoureu Avatar answered Nov 15 '22 12:11

PRMoureu


I was still getting the same error after import mysql.connector and checking permissions. I was running my script on MacOS using bash. After hours of searching for a solution, came across this post on the mysql forums: https://forums.mysql.com/read.php?50,584171,584729#msg-584729

The poster mentions that your python script can't be named mysql.py My script was named types.py, so I renamed it to setup-types.py and that solved my problem. Hopefully this saves others some time when dealing with this same error.

like image 8
Gail Sparks Avatar answered Nov 15 '22 13:11

Gail Sparks


One more observation. In many tutorials, they didn't installed the python connector the same way. At those times, the import statement might be the cause of the issue. Try the below import statement.

import mysql.connector as mysql
like image 2
Surya Avatar answered Nov 15 '22 12:11

Surya


I had "pip install"ed mysql-connector before installing mysql-connector-python. I uninstalled the mysql-connector, and was getting this error.

As this stackoverflow question/answer mentions, uninstalling and then reinstalling the mysql-connector-python had everything working.

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

like image 2
Gerard ONeill Avatar answered Nov 15 '22 12:11

Gerard ONeill