def _syspath_modname_to_modpath(modname, sys_path=None, exclude=None):
def _isvalid(modpath, base):<EOL><INDENT>subdir = dirname(modpath)<EOL>while subdir and subdir != base:<EOL><INDENT>if not exists(join(subdir, '<STR_LIT>')):<EOL><INDENT>return False<EOL><DEDENT>subdir = dirname(subdir)<EOL><DEDENT>return True<EOL><DEDENT>_fname_we = modname.replace('<STR_LIT:.>', os.path.sep)<EOL>candidate_fnames = [<EOL>_fname_we + '<STR_LIT>',<EOL>]<EOL>candidate_fnames += [_fname_we + ext for ext in _platform_pylib_exts()]<EOL>if sys_path is None:<EOL><INDENT>sys_path = sys.path<EOL><DEDENT>candidate_dpaths = ['<STR_LIT:.>' if p == '<STR_LIT>' else p for p in sys_path]<EOL>if exclude:<EOL><INDENT>def normalize(p):<EOL><INDENT>if sys.platform.startswith('<STR_LIT:win32>'):  <EOL><INDENT>return realpath(p).lower()<EOL><DEDENT>else:<EOL><INDENT>return realpath(p)<EOL><DEDENT><DEDENT>real_exclude = {normalize(p) for p in exclude}<EOL>candidate_dpaths = [p for p in candidate_dpaths<EOL>if normalize(p) not in real_exclude]<EOL><DEDENT>for dpath in candidate_dpaths:<EOL><INDENT>modpath = join(dpath, _fname_we)<EOL>if exists(modpath):<EOL><INDENT>if isfile(join(modpath, '<STR_LIT>')):<EOL><INDENT>if _isvalid(modpath, dpath):<EOL><INDENT>return modpath<EOL><DEDENT><DEDENT><DEDENT>for fname in candidate_fnames:<EOL><INDENT>modpath = join(dpath, fname)<EOL>if isfile(modpath):<EOL><INDENT>if _isvalid(modpath, dpath):<EOL><INDENT>return modpath<EOL><DEDENT><DEDENT><DEDENT><DEDENT>
syspath version of modname_to_modpath

Args:
    modname (str): name of module to find
    sys_path (List[PathLike], default=None):
        if specified overrides `sys.path`
    exclude (List[PathLike], default=None):
        list of directory paths. if specified prevents these directories
        from being searched.

Notes:
    This is much slower than the pkgutil mechanisms.

CommandLine:
    python -m xdoctest.static_analysis _syspath_modname_to_modpath

Example:
    >>> print(_syspath_modname_to_modpath('xdoctest.static_analysis'))
    ...static_analysis.py
    >>> print(_syspath_modname_to_modpath('xdoctest'))
    ...xdoctest
    >>> print(_syspath_modname_to_modpath('_ctypes'))
    ..._ctypes...
    >>> assert _syspath_modname_to_modpath('xdoctest', sys_path=[]) is None
    >>> assert _syspath_modname_to_modpath('xdoctest.static_analysis', sys_path=[]) is None
    >>> assert _syspath_modname_to_modpath('_ctypes', sys_path=[]) is None
    >>> assert _syspath_modname_to_modpath('this', sys_path=[]) is None

Example:
    >>> # test what happens when the module is not visible in the path
    >>> modname = 'xdoctest.static_analysis'
    >>> modpath = _syspath_modname_to_modpath(modname)
    >>> exclude = [split_modpath(modpath)[0]]
    >>> found = _syspath_modname_to_modpath(modname, exclude=exclude)
    >>> # this only works if installed in dev mode, pypi fails
    >>> assert found is None, 'should not have found {}'.format(found)