NameError: global name 'MySQLdb' is not defined
<p><strong>code for db.py</strong></p>

<pre><code>class n2b_db:
    ''' database function class '''

    database=connectiox=cursor=server=None

def __init__(self,server,database):
    self.database   = database
    self.server     = server

@classmethod
def connect(cls,self):
    self.connectiox = MySQLdb.connect(host=self.server,user="root", passwd="samsam",db=self.database)
    self.cursor     = self.connectiox.cursor()
    print("connection successful")

@classmethod    
def disconnect(cls,self):
    self.connectiox.close
    print("connection closed")

@classmethod
def query(cls,self, sqlstatement, params):
    if (params is not None):
        rtnvalue = self.cursor.execute(sqlstatement, (params,))
    else:
        rtnvalue = self.cursor.execute(sqlstatement)

    try:
        self.connectiox.commit()
        print("transaction committed")
        return rtnvalue
    except:
        self.connectiox.rollback() 
        print("transaction rolled back")
        return None
</code></pre>

<p><strong>this is sample code to reproduce the error i am getting</strong> </p>

<pre><code>import MySQLdb
from passlib.hash import sha256_crypt
from db import *
import gc

username  ="John"
email     ="john@abc.com"
password  =sha256_crypt.encrypt((str("john01")))

x = n2b_db("localhost","pythondb")
x.connect()
n = x.query("""Select * from users where username=%s""",username)

if int(n)&gt;0:
    print("That username is already taken, please choose another")
else:
    Print("trying to write to sql")
    n = x.query("""Insert into users(username,password,email,tracking) values (%s,%s,%s,%s)""",username,password,email,"Test tracking")
    Print("Thanks for registering")
    gc.collect()
</code></pre>

<p>When i run this code i am getting error as below and not sure why i am getting this error. </p>

<pre><code>&gt;&gt;&gt; x.connect()
Traceback (most recent call last):
  File "&lt;stdin&gt;", line 1, in &lt;module&gt;
  File "/var/www/FlaskApp/FlaskApp/classes/db.py", line 12, in connect
    self.connectiox = MySQLdb.connect(host=self.server,user="root", passwd="samsam",db=self.database)
NameError: global name 'MySQLdb' is not defined
</code></pre>