Import module inside Python class

Import module inside Python class

Inside a function, local variables are stored in numbered slots, rather than in a dictionary the way global variables are stored. This means the Python compiler needs to know all the names of the functions local variables before it creates the functions code object, so it can translate each local variable access to an appropriate bytecode to read or write from the slot.

When you do from some_module import *, the exact names that get imported cant be known ahead of time. Its up to some_module what names it defines (or puts in __all__, if it has one). This means the compiler cant see the names ahead of time and assign each name to a slot. Not knowing the names up front is not an issue when importing at the top level, as each name/object pair will just be added to the modules globals dictionary.

To make your code work, you either need to import just the module (and access the modules contents as attributes) or you can explicitly name the items you want to import from it. Which is best depends on your taste (and perhaps how many different names you need from the modules).

I dont know what exactly you need from your i2c_helper_whatever modules, so I cant give an example of explicitly listed names, but heres an example that uses an as clause on a regular import to give the same name (in the local namespace) to whatever module is selected (I dont know if the name I picked is a good one, use your own judgement):

def __init__(self, TCON_COMMS_METHOD=0):
        print(Using Aardvark for TCON communication.)
        import i2c_helper_aardvark as i2c_helper
    elif self.TCON_COMMS_METHOD == self.TCON_COMMS_GPU:
        print(Using AUX for TCON communication.)
        import i2c_helper_aux as i2c_helper
        print(Using SMC for TCON communication.)
        import i2c_helper_smc as i2c_helper

    i2c_helper.something() # this will call a function from whichever module was imported

It means that you cant use from module import * inside a class, function, or otherwise. Move them to the top of the file.

Import module inside Python class

Leave a Reply

Your email address will not be published. Required fields are marked *