def get_all_imports(path, encoding=None, extra_ignore_dirs=None):
    imports = set()
    raw_imports = set()
    candidates = []
    ignore_errors = False
    ignore_dirs = ['.hg', '.svn', '.git', '.tox', '__pycache__', 'env', 'venv']
    if extra_ignore_dirs:
        ignore_dirs_parsed = []
        for e in extra_ignore_dirs:
            ignore_dirs_parsed.append(os.path.basename(os.path.realpath(e)))
        ignore_dirs.extend(ignore_dirs_parsed)
    for (root, dirs, files) in os.walk(path):
        dirs[:] = [d for d in dirs if (d not in ignore_dirs)]
        candidates.append(os.path.basename(root))
        files = [fn for fn in files if (os.path.splitext(fn)[1] == '.py')]
        candidates += [os.path.splitext(fn)[0] for fn in files]
        for file_name in files:
            with open_func(os.path.join(root, file_name), 'r', encoding=encoding) as f:
                contents = f.read()
                try:
                    tree = ast.parse(contents)
                    for node in ast.walk(tree):
                        if isinstance(node, ast.Import):
                            for subnode in node.names:
                                raw_imports.add(subnode.name)
                        elif isinstance(node, ast.ImportFrom):
                            raw_imports.add(node.module)
                except Exception as exc:
                    if ignore_errors:
                        traceback.print_exc(exc)
                        logging.warn(('Failed on file: %s' % os.path.join(root, file_name)))
                        continue
                    else:
                        logging.error(('Failed on file: %s' % os.path.join(root, file_name)))
                        raise exc
    for name in [n for n in raw_imports if n]:
        (cleaned_name, _, _) = name.partition('.')
        imports.add(cleaned_name)
    packages = (set(imports) - set((set(candidates) & set(imports))))
    logging.debug('Found packages: {0}'.format(packages))
    with open(join('stdlib'), 'r') as f:
        data = [x.strip() for x in f.readlines()]
        data = ([x for x in data if (x not in py2_exclude)] if py2 else data)
        return sorted(list((set(packages) - set(data))))
def get_all_imports(path, encoding=None, extra_ignore_dirs=None):
    imports = set()
    raw_imports = set()
    candidates = []
    ignore_errors = False
    ignore_dirs = ['.hg', '.svn', '.git', '.tox', '__pycache__', 'env', 'venv']
    if extra_ignore_dirs:
        ignore_dirs_parsed = []
        for e in extra_ignore_dirs:
            ignore_dirs_parsed.append(os.path.basename(os.path.realpath(e)))
        ignore_dirs.extend(ignore_dirs_parsed)
    for (root, dirs, files) in os.walk(path):
        dirs[:] = [d for d in dirs if (d not in ignore_dirs)]
        candidates.append(os.path.basename(root))
        files = [fn for fn in files if (os.path.splitext(fn)[1] == '.py')]
        candidates += [os.path.splitext(fn)[0] for fn in files]
        for file_name in files:
            with open_func(os.path.join(root, file_name), 'r', encoding=encoding) as f:
                contents = f.read()
                try:
                    tree = ast.parse(contents)
                    for node in ast.walk(tree):
                        if isinstance(node, ast.Import):
                            for subnode in node.names:
                                raw_imports.add(subnode.name)
                        elif isinstance(node, ast.ImportFrom):
                            raw_imports.add(node.module)
                except Exception as exc:
                    if ignore_errors:
                        traceback.print_exc(exc)
                        logging.warn(('Failed on file: %s' % os.path.join(root, file_name)))
                        continue
                    else:
                        logging.error(('Failed on file: %s' % os.path.join(root, file_name)))
                        raise exc
    for name in [n for n in raw_imports if n]:
        (cleaned_name, _, _) = name.partition('.')
        imports.add(cleaned_name)
    packages = (set(imports) - set((set(candidates) & set(imports))))
    logging.debug('Found packages: {0}'.format(packages))
    with open(join('stdlib'), 'r') as f:
        data = [x.strip() for x in f.readlines()]
        data = ([x for x in data if (x not in py2_exclude)] if py2 else data)
        return sorted(list((set(packages) - set(data))))