ou need to import MySQLdb in db.py instead of sample code, otherwise in db.py the interpreter cannot understand what is MySQLdb. You can check Python modules for more details. Hope it helps. https://stackoverflow.com/questions/35712562/nameerror-global-name-mysqldb-is-not-defined