Load a Python module from a path under a specified name.

        Parameters:
            modulename (str): Fully qualified module name, e.g. ``x.y.z``.
            modulepath (str): Filename of the module.

        Returns:
            Loaded module.

def load_module(modulename, modulepath):
    """ Load a Python module from a path under a specified name.

        Parameters:
            modulename (str): Fully qualified module name, e.g. ``x.y.z``.
            modulepath (str): Filename of the module.

        Returns:
            Loaded module.
    """
    if '.' in modulename:
        modulepackage, modulebase = modulename.rsplit('.', 1)
    else:
        modulepackage = ''

    imp.acquire_lock()
    try:
        # Check if module is already loaded
        if modulename not in sys.modules:
            # Find module on disk and try to load it
            path, name = os.path.split(modulepath)
            name = os.path.splitext(name)[0]
            handle, path, info = imp.find_module(name, [path])
            try:
                # Load the module and put into sys.modules
                module = imp.load_module(modulename, handle, path, info)
                if modulepackage:
                    setattr(sys.modules[modulepackage], modulebase, module)
            finally:
                # Make sure handle is closed properly
                if handle:
                    handle.close()
    finally:
        imp.release_lock()

    return sys.modules[modulename]