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

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.


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.