Forums

server log saying: name 'MySQLdb' is not defined

Hello, I am trying to connect to a MySQL-database but have no success. Code runs per Flask in a web app, no virtual env involved. The entry in the server log reads: name 'MySQLdb' is not defined. Credentials should be OK, since entering them in Python-Shell connection could be established. The import of MySQLdb is set in a try/except-clause, since formerly messages claimed uncaught exception. With or without clause result has same outcome. Relevant code reads:

#### module which receives the sensor data and stores it in a database
from flask import Flask
try:
    import MySQLdb
except Exception as e:
    print("Import MySQLdb failed .....", e)


def checkDBConnection():
    try:
        db = MySQLdb(app)
        cnx = db.connect(
        host = '<userName>.mysql.eu.pythonanywhere-services.com',
        user = '<userName>',
        password = '*******',
        database = '<userName>$sensorData'
        )
        if cnx.is_connected():
            return True
        else:
            return False
    except Exception as e:
        print("Database connection error: ", e)
        return False

app = Flask(__name__)
#application = Flask(__name__)   # see hint in *.wsgi file

@app.route("/sensorData/<val>")
def storeDB(val):
    return f'val is: {val}'

@app.route("/accDB")
def accessDB():
    check = checkDBConnection()
    if check:
        return "<p>Database access successful...........</p>"
        # closing database connection
        #cnx.close()
    else:
        return "<p>Connection failed ......</p>"

Would be glad, if one could help. Thank you!

It's not defined because it is failing to import. Hiding the exception does not change that. MySQLdb is a very old module and is not available in our default installs. You can use mysql connector

Hello Glenn, thank you for your reply. I changed to mysql.connector, now w/o try/except-clause, but import fails either. Below the error log.

2025-06-13 20:48:16,514: Error running WSGI application
2025-06-13 20:48:16,516: ModuleNotFoundError: No module named 'mysql'
2025-06-13 20:48:16,517:   File "/var/www/<myUserName>_eu_pythonanywhere_com_wsgi.py", line 17, in <module>
2025-06-13 20:48:16,517:     from flask_app import app as application  # noqa
2025-06-13 20:48:16,517:     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-06-13 20:48:16,517: 
2025-06-13 20:48:16,517:   File "/home/<myUserName>/flask_app.py", line 3, in <module>
2025-06-13 20:48:16,517:     import mysql.connector
2025-06-13 20:48:16,518: ***************************************************
2025-06-13 20:48:16,518: If you're seeing an import error and don't know why,
2025-06-13 20:48:16,518: we have a dedicated help page to help you debug: 
2025-06-13 20:48:16,518: https://help.pythonanywhere.com/pages/DebuggingImportError/
2025-06-13 20:48:16,518: ***************************************************
2025-06-13 20:48:16,767: Error running WSGI application
2025-06-13 20:48:16,768: ModuleNotFoundError: No module named 'mysql'
2025-06-13 20:48:16,768:   File "/var/www/<myUserName>_eu_pythonanywhere_com_wsgi.py", line 17, in <module>
2025-06-13 20:48:16,768:     from flask_app import app as application  # noqa
2025-06-13 20:48:16,768:     ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
2025-06-13 20:48:16,768: 
2025-06-13 20:48:16,769:   File "/home/<myUserName>/flask_app.py", line 3, in <module>
2025-06-13 20:48:16,769:     import mysql.connector
2025-06-13 20:48:16,769: ***************************************************
2025-06-13 20:48:16,769: If you're seeing an import error and don't know why,
2025-06-13 20:48:16,769: we have a dedicated help page to help you debug: 
2025-06-13 20:48:16,769: https://help.pythonanywhere.com/pages/DebuggingImportError/
2025-06-13 20:48:16,769: ***************************************************

One more aspect came to my attention. Its the entry in the *.wsgi file. I understand that Line 17 is saying, that the app shall be called as application. But changing this to the routes will also make no difference.

Try following the DebuggingImportError help page that we link to in the error log. Make sure that you have the package installed when your code expecrs it (eg for the right python version or virtual environment) and that you import it correctly (see the docs of the module).

Hello fjl, finally I solved the problem. It was a path matter. After I added the path '/usr/local/lib/python3.13/site-packages' in the *wsgi.py file the module mysql.connector was found. Thank you for your help. Kind regards

I'm glad you were able to solve it!