# Buggy code
def main() -> None:
    options = parse_options(sys.argv[1:])
    if not os.path.isdir(options.output_dir):
        raise SystemExit('Directory "{}" does not exist'.format(options.output_dir))
    if options.recursive and options.no_import:
        raise SystemExit(
            "recursive stub generation without importing is not currently supported"
        )
    sigs = {}  # type: Any
    class_sigs = {}  # type: Any
    if options.doc_dir:
        all_sigs = []  # type: Any
        all_class_sigs = []  # type: Any
        for path in glob.glob("%s/*.rst" % options.doc_dir):
            with open(path) as f:
                func_sigs, class_sigs = parse_all_signatures(f.readlines())
            all_sigs += func_sigs
            all_class_sigs += class_sigs
        sigs = dict(find_unique_signatures(all_sigs))
        class_sigs = dict(find_unique_signatures(all_class_sigs))
    for module in (
        options.modules if not options.recursive else walk_packages(options.modules)
    ):
        try:
            generate_stub_for_module(
                module,
                output_dir=options.output_dir,
                add_header=True,
                sigs=sigs,
                class_sigs=class_sigs,
                pyversion=options.pyversion,
                no_import=options.no_import,
                search_path=options.search_path,
                interpreter=options.interpreter,
                include_private=options.include_private,
            )
        except Exception as e:
            if not options.ignore_errors:
                raise e
            else:
                print("Stub generation failed for", module, file=sys.stderr)


# Traceback (ModuleNotFoundError)
test>pip install git+https://github.com/python/mypy
test>tree /f
Folder PATH listing
C:.
└───pkg
test.py
__init__.py
test>stubgen pkg
Traceback (most recent call last):
File "c:\python36\lib\site-packages\mypy\stubgen.py", line 150, in find_module_path_and_all
mod = importlib.import_module(module)
File "c:\python36\lib\importlib\__init__.py", line 126, in import_module
return _bootstrap._gcd_import(name[level:], package, level)
File "<frozen importlib._bootstrap>", line 994, in _gcd_import
File "<frozen importlib._bootstrap>", line 971, in _find_and_load
File "<frozen importlib._bootstrap>", line 953, in _find_and_load_unlocked
ModuleNotFoundError: No module named 'pkg'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "c:\python36\lib\site-packages\mypy\stubgen.py", line 102, in generate_stub_for_module
interpreter=interpreter)
File "c:\python36\lib\site-packages\mypy\stubgen.py", line 152, in find_module_path_and_all
raise CantImport(module)
mypy.stubgen.CantImport: pkg
Failed to import pkg; skipping it

# Fixed code
def main() -> None:
    # Make sure that the current directory is in sys.path so that
    # stubgen can be run on packages in the current directory.
    if "" not in sys.path:
        sys.path.insert(0, "")

    options = parse_options(sys.argv[1:])
    if not os.path.isdir(options.output_dir):
        raise SystemExit('Directory "{}" does not exist'.format(options.output_dir))
    if options.recursive and options.no_import:
        raise SystemExit(
            "recursive stub generation without importing is not currently supported"
        )
    sigs = {}  # type: Any
    class_sigs = {}  # type: Any
    if options.doc_dir:
        all_sigs = []  # type: Any
        all_class_sigs = []  # type: Any
        for path in glob.glob("%s/*.rst" % options.doc_dir):
            with open(path) as f:
                func_sigs, class_sigs = parse_all_signatures(f.readlines())
            all_sigs += func_sigs
            all_class_sigs += class_sigs
        sigs = dict(find_unique_signatures(all_sigs))
        class_sigs = dict(find_unique_signatures(all_class_sigs))
    for module in (
        options.modules if not options.recursive else walk_packages(options.modules)
    ):
        try:
            generate_stub_for_module(
                module,
                output_dir=options.output_dir,
                add_header=True,
                sigs=sigs,
                class_sigs=class_sigs,
                pyversion=options.pyversion,
                no_import=options.no_import,
                search_path=options.search_path,
                interpreter=options.interpreter,
                include_private=options.include_private,
            )
        except Exception as e:
            if not options.ignore_errors:
                raise e
            else:
                print("Stub generation failed for", module, file=sys.stderr)