python lazy importing

Was wondering how to stop my little Python program from crashing if it fails to import MySql driver, even when the functionality with the MySql driver is not invoked. The Python runtime seems to try to load all modules that are imported as soon as they are referenced. Including if you import the module from another module. Generally this happens always as something will refer to the module or it is a useless module.

The fix is really simple. Instead of

import mysql.connector

at the top of the file, I should just import it where it is first used, such as

def __init__(self):
    import mysql.connector

Seems the term I was looking for in Google was “Python lazy importing” or similar.. whooppee 🙂


Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s