AttributeError: module ‘mysql.connector’ has no attribute ‘cursor_cext’

I am getting the following error when I try to connect to my database:


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

    import mysql
    import mysql.connector  
    from mysql.connector import errorcode 
    # MySQL database access parameters
    DB_PARAMS = {
        "user": "root",
        "password": "letmego",
        "host": "localhost",
        "port": 3306,
        "raise_on_warnings": False,
        "database": "playground"
    def connect_db(
        params: dict,
        verbose: bool = True
    ) -> mysql.connector.connection_cext.CMySQLConnection:
        conn = mysql.connector.connect(**params)
    except mysql.connector.Error as err:
        print("[!] Function 'connect_db'")
        if verbose:
            print(f"Database '{params['database']}' successfully connected.n")
        return conn

if __name__ == '__main__':

    # connect to MySQL
    conn = connect_db(DB_PARAMS)

Please advise what I did wrong here. I also think I have imported the right dependencies.


The connect_db method doesn’t return the type you’re saying it does. As you can see in it’s code here it returns a mysql.connector.connection.MySQLConnection object and not a mysql.connector.connection_cext.CMySQLConnection. You can fix your code by importing the right class using:

from mysql.connector.connection import MySQLConnection

And then replacing the typing on your connect_db method to:

def connect_db(
        params: dict,
        verbose: bool = True
    ) -> MySQLConnection: