def find_requirement(self, req, upgrade, ignore_compatibility=False):<EOL>
all_candidates = self.find_all_candidates(req.name)<EOL>compatible_versions = set(<EOL>req.specifier.filter(<EOL>[str(c.version) for c in all_candidates],<EOL>prereleases=(<EOL>self.allow_all_prereleases<EOL>if self.allow_all_prereleases else None<EOL>),<EOL>)<EOL>)<EOL>applicable_candidates = [<EOL>c for c in all_candidates if str(c.version) in compatible_versions<EOL>]<EOL>if applicable_candidates:<EOL><INDENT>best_candidate = max(applicable_candidates,<EOL>key=self._candidate_sort_key)<EOL><DEDENT>else:<EOL><INDENT>best_candidate = None<EOL><DEDENT>if req.satisfied_by is not None:<EOL><INDENT>installed_version = parse_version(req.satisfied_by.version)<EOL><DEDENT>else:<EOL><INDENT>installed_version = None<EOL><DEDENT>if installed_version is None and best_candidate is None:<EOL><INDENT>logger.critical(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>req,<EOL>'<STR_LIT:U+002CU+0020>'.join(<EOL>sorted(<EOL>{str(c.version) for c in all_candidates},<EOL>key=parse_version,<EOL>)<EOL>)<EOL>)<EOL>raise DistributionNotFound(<EOL>'<STR_LIT>' % req<EOL>)<EOL><DEDENT>best_installed = False<EOL>if installed_version and (<EOL>best_candidate is None or<EOL>best_candidate.version <= installed_version):<EOL><INDENT>best_installed = True<EOL><DEDENT>if not upgrade and installed_version is not None:<EOL><INDENT>if best_installed:<EOL><INDENT>logger.debug(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>installed_version,<EOL>)<EOL><DEDENT>else:<EOL><INDENT>logger.debug(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>installed_version,<EOL>best_candidate.version,<EOL>)<EOL><DEDENT>return None<EOL><DEDENT>if best_installed:<EOL><INDENT>logger.debug(<EOL>'<STR_LIT>'<EOL>'<STR_LIT>',<EOL>installed_version,<EOL>'<STR_LIT:U+002CU+0020>'.join(sorted(compatible_versions, key=parse_version)) or<EOL>"<STR_LIT:none>",<EOL>)<EOL>raise BestVersionAlreadyInstalled<EOL><DEDENT>logger.debug(<EOL>'<STR_LIT>',<EOL>best_candidate.version,<EOL>'<STR_LIT:U+002CU+0020>'.join(sorted(compatible_versions, key=parse_version))<EOL>)<EOL>return best_candidate.location<EOL>
Try to find a Link matching req

        Expects req, an InstallRequirement and upgrade, a boolean
        Returns a Link if found,
        Raises DistributionNotFound or BestVersionAlreadyInstalled otherwise