Python's "global" variables don't have global scope. They are module-scope variables. So the same-named global in different modules isn't the same variable.
I think you might be closing mainlib.db and then setting mytestcode.db to a new database. All the rest of your code of course continues to use mainlib.db, which is now closed.
Try mainlib.db = MySQLdb.connect(...), and the same for the cursor. Directly modifying another module's variables is ugly, but it works as you'd expect.
The alternative would be to introduce a way of configuring how mainlib opens the DB. For example, you could have a function like this in mainlib:
db = None
dbname = None
cursor = None
def connectdb(name = None):
"""
Set up the global database connection and cursor, if it isn't already.
Omit 'name' when the caller doesn't care what database is used,
and is happy to accept whatever database is already connected or
connect to a default database.
Since there cannot be multiple global databases, an exception is thrown
if 'name' is specified, the global connection already exists, and the
names don't match.
"""
global db, dbname, cursor
if db is None:
if name is None:
name = 'MyDatabase'
db = MySQLdb.connect(host='localhost', user='admin', password='admin', db=name)
dbname = name
cursor = db.cursor()
elif name not in (None, dbname):
raise Exception('cannot connect to the specified db: the global connection already exists and connects to a different db')
Now, in your normal program (not in every module, just the top level) you call mainlib.connectdb() right after importing mainlib. In your test code you call mainlib.connectdb('TestDatabase').
Optionally, you could have connectdb return the cursor and/or the connection object, That way, everything that uses the global db can go through this function.
Personally, I prefer not to use globals for this at all -- I would have a function to create a database connection and I would pass that database as a parameter into anything that needs it. However, I realise that tastes vary in this respect.
mainlibas a prettier solution, let me know!