import inspect


def get_module(stack_level):
    """
        Get a reference to the module at some level of the stack.
    """
    stack = inspect.stack()
    if stack_level >= len(stack):
        raise Exception("Stack level was to large.")

    #+1 because this function is on the stack!
    frame = inspect.stack()[stack_level+1]

    #Get the module from the right frame.
    
    mod = inspect.getmodule(frame[0])
    return mod