3

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?

3
  • Can you fix your indentation? Commented Dec 19, 2019 at 17:52
  • 4
    SQLAlchemy doesn't access database directly but uses other modules to work with different databases. And you need some of module which works with mysql - ie. mysql-connector-python or check what module shows your pip Commented Dec 19, 2019 at 17:52
  • @furas The server does not have mysql-connector-python module. I'll check and report back. Commented Dec 19, 2019 at 17:59

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

you need to install mysql-connector-python

do pip install mysql-connector-python

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.