def package_modpaths(pkgpath, with_pkg=False, with_mod=True, followlinks=True,<EOL>recursive=True, with_libs=False, check=True):
if isfile(pkgpath):<EOL><INDENT>yield pkgpath<EOL><DEDENT>else:<EOL><INDENT>if with_pkg:<EOL><INDENT>root_path = join(pkgpath, '<STR_LIT>')<EOL>if not check or exists(root_path):<EOL><INDENT>yield root_path<EOL><DEDENT><DEDENT>valid_exts = ['<STR_LIT>']<EOL>if with_libs:<EOL><INDENT>valid_exts += _platform_pylib_exts()<EOL><DEDENT>for dpath, dnames, fnames in os.walk(pkgpath, followlinks=followlinks):<EOL><INDENT>ispkg = exists(join(dpath, '<STR_LIT>'))<EOL>if ispkg or not check:<EOL><INDENT>check = True  <EOL>if with_mod:<EOL><INDENT>for fname in fnames:<EOL><INDENT>if splitext(fname)[<NUM_LIT:1>] in valid_exts:<EOL><INDENT>if fname != '<STR_LIT>':<EOL><INDENT>path = join(dpath, fname)<EOL>yield path<EOL><DEDENT><DEDENT><DEDENT><DEDENT>if with_pkg:<EOL><INDENT>for dname in dnames:<EOL><INDENT>path = join(dpath, dname, '<STR_LIT>')<EOL>if exists(path):<EOL><INDENT>yield path<EOL><DEDENT><DEDENT><DEDENT><DEDENT>else:<EOL><INDENT>del dnames[:]<EOL><DEDENT>if not recursive:<EOL><INDENT>break<EOL><DEDENT><DEDENT><DEDENT>
r"""
    Finds sub-packages and sub-modules belonging to a package.

    Args:
        pkgpath (str): path to a module or package
        with_pkg (bool): if True includes package __init__ files (default =
            False)
        with_mod (bool): if True includes module files (default = True)
        exclude (list): ignores any module that matches any of these patterns
        recursive (bool): if False, then only child modules are included
        with_libs (bool): if True then compiled shared libs will be returned as well
        check (bool): if False, then then pkgpath is considered a module even
            if it does not contain an __init__ file.

    Yields:
        str: module names belonging to the package

    References:
        http://stackoverflow.com/questions/1707709/list-modules-in-py-package

    Example:
        >>> from xdoctest.static_analysis import *
        >>> pkgpath = modname_to_modpath('xdoctest')
        >>> paths = list(package_modpaths(pkgpath))
        >>> print('\n'.join(paths))
        >>> names = list(map(modpath_to_modname, paths))
        >>> assert 'xdoctest.core' in names
        >>> assert 'xdoctest.__main__' in names
        >>> assert 'xdoctest' not in names
        >>> print('\n'.join(names))