def modpath_to_modname(modpath, hide_init=True, hide_main=False, check=True,<EOL>relativeto=None):
if check and relativeto is None:<EOL><INDENT>if not exists(modpath):<EOL><INDENT>raise ValueError('<STR_LIT>'.format(modpath))<EOL><DEDENT><DEDENT>modpath_ = abspath(expanduser(modpath))<EOL>modpath_ = normalize_modpath(modpath_, hide_init=hide_init,<EOL>hide_main=hide_main)<EOL>if relativeto:<EOL><INDENT>dpath = dirname(abspath(expanduser(relativeto)))<EOL>rel_modpath = relpath(modpath_, dpath)<EOL><DEDENT>else:<EOL><INDENT>dpath, rel_modpath = split_modpath(modpath_, check=check)<EOL><DEDENT>modname = splitext(rel_modpath)[<NUM_LIT:0>]<EOL>if '<STR_LIT:.>' in modname:<EOL><INDENT>modname, abi_tag = modname.split('<STR_LIT:.>')<EOL><DEDENT>modname = modname.replace('<STR_LIT:/>', '<STR_LIT:.>')<EOL>modname = modname.replace('<STR_LIT:\\>', '<STR_LIT:.>')<EOL>return modname<EOL>
Determines importable name from file path

Converts the path to a module (__file__) to the importable python name
(__name__) without importing the module.

The filename is converted to a module name, and parent directories are
recursively included until a directory without an __init__.py file is
encountered.

Args:
    modpath (str): module filepath
    hide_init (bool): removes the __init__ suffix (default True)
    hide_main (bool): removes the __main__ suffix (default False)
    check (bool): if False, does not raise an error if modpath is a dir
        and does not contain an __init__ file.
    relativeto (str, optional): if specified, all checks are ignored and
        this is considered the path to the root module.

Returns:
    str: modname

Raises:
    ValueError: if check is True and the path does not exist

CommandLine:
    xdoctest -m xdoctest.static_analysis modpath_to_modname

Example:
    >>> from xdoctest import static_analysis
    >>> modpath = static_analysis.__file__.replace('.pyc', '.py')
    >>> modpath = modpath.replace('.pyc', '.py')
    >>> modname = modpath_to_modname(modpath)
    >>> assert modname == 'xdoctest.static_analysis'

Example:
    >>> import xdoctest
    >>> assert modpath_to_modname(xdoctest.__file__.replace('.pyc', '.py')) == 'xdoctest'
    >>> assert modpath_to_modname(dirname(xdoctest.__file__.replace('.pyc', '.py'))) == 'xdoctest'

Example:
    >>> modpath = modname_to_modpath('_ctypes')
    >>> modname = modpath_to_modname(modpath)
    >>> assert modname == '_ctypes'