Checking module name inside 'except ImportError'

I think you misunderstood the warning, if you do not define a variable called MySQLdb in the except block then later on when you try to use the module you would get a NameError:

try:
    import foo
except ImportError:
    pass

foo.say_foo() #foo may or may not be defined at this point!

If the module is only used in the try: clause then this is no issue. But for the more general case the checker expects you to define the variable in the except block:

try:
    import foo
except ImportError:
    foo = None  #now foo always exists

if foo: #if the module is present
    foo.say_foo()
else:
    print("foo") #backup use

If the module is only used in the try block then you can indicate to the checker (and yourself) that you can't use the module later by deleting it from the namespace:

try:
    import foo
except ImportError:
    pass
else:
    # if it was able to import use it then get rid of the local variable
    foo.do_thing()
    del foo #optional module should not be relied on outside 
 

# now foo never exists here, checker is happy.

In Python 3.3+, an ImportError has the attribute name that tells the name of the module whose import failed. Then of course MySQLdb would hint that you're stuck with Python 2.