Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLAlchemy: No module named 'mysql'

I have created this very simple script that connects with SQLAlchemy to a MySQL database and runs a SELECT statement. It works great on my dev laptop. Now that I moved it to a new server, it breaks.

from sqlalchemy import create_engine
import pandas as pd


def return_connection_string(database_name, database_user, database_password, database_host, database_port):
    try:
        connection_string = 'mysql+mysqlconnector://' + database_user + ':' + \
            database_password + '@' + database_host + ':' + database_port + '/' + database_name
        print(connection_string)
        return connection_string
    except Exception as e:
        print('Encountered error while generating connection string for MySQL!')
        print(e)


def return_connection_object(database_name, database_user, database_password, database_host, database_port):
    try:
        connection_string = return_connection_string(
            database_name, database_user, database_password, database_host, database_port)
        engine = create_engine(connection_string).connect()
        return engine
    except Exception as e:
        print('Encountered error while connecting to MySQL database!')
        print(e)


mysql_conn = return_connection_object(
    'xyz', 'xyz', 'xyz', 'xyz', 'xyz')


SQL_Query = pd.read_sql_query(
    'select current_date as today, current_date-1 as yesterday', mysql_conn)

df = pd.DataFrame(SQL_Query, columns=['today', 'yesterday'])

df1 = df[['yesterday']]

print(df1)

mysql_conn.close()

At runtime, the script fails with this error

Encountered error while connecting to MySQL database!
No module named 'mysql'
AttributeError: 'NoneType' object has no attribute 'cursor' 

I compared my server against my dev laptop and PIP shows me the same version of SQLAlchemy module: 1.3.11.

Do I need to install another module? What am I missing?

like image 926
A B Avatar asked Jul 18 '26 12:07

A B


1 Answers

After reviewing the output of PIP on my server and my dev laptop, I discovered that one module was missing. Running this command

pip3 install --upgrade mysql-connector-python

solved the problem.

like image 79
A B Avatar answered Jul 21 '26 01:07

A B