def infer_module_name(filename, fspath):
    """Convert a python filename to a module relative to pythonpath."""
    filename, _ = os.path.splitext(filename)
    for f in fspath:
        short_name = f.relative_path(filename)
        if short_name:
            # The module name for __init__.py files is the directory.
            if short_name.endswith(os.path.sep + "__init__"):
                short_name = short_name[:short_name.rfind(os.path.sep)]
            return short_name.replace(os.path.sep, '.')
    # We have not found filename relative to anywhere in pythonpath.
    return ''
Convert a python filename to a module relative to pythonpath.