def find_package(name, installed, package=False):
    """Finds a package in the installed list.

    If `package` is true, match package names, otherwise, match import paths.
    """
    if package:
        name = name.lower()
        tests = lambda x: x.user and name == x.name.lower(
            ), lambda x: x.local and name == x.name.lower(
            ), lambda x: name == x.name.lower()
    else:
        tests = (lambda x: x.user and name in x.import_names, lambda x: x.
            local and name in x.import_names, lambda x: name in x.import_names)
    for t in tests:
        try:
            found = list(filter(t, installed))
            if found and not found[0].is_scan:
                return found[0]
        except StopIteration:
            pass
    return None
