import imp
from importlib.machinery import SourceFileLoader
import six


def load_module_from_file(name, filepath):
    """Load a python module from a sourcefile.

    Args:
        name (str): Module name.
        filepath (str): Python sourcefile.

    Returns:
        `module`: Loaded module.
    """
    if six.PY2:
        with open(filepath) as f:
            return imp.load_source(name, filepath, f)

    else:
        return SourceFileLoader(name, filepath).load_module()