def test_find_python_in_path(tmp_path): assert utils.find_python_in_path(sys.executable) == pathlib.Path( sys.executable) posix_path_to_executable = pathlib.Path(sys.executable).as_posix().lower() if sys.platform == "darwin": found_version_of_executable = re.split(r"(python@[\d.]*\d+)", posix_path_to_executable) posix_path_to_executable = "".join(found_version_of_executable[0:2]) assert (utils.find_python_in_path( sys.prefix).as_posix().lower().startswith(posix_path_to_executable)) assert not utils.find_python_in_path(tmp_path)
def find_interpreters(self, python_spec: str | None = None ) -> Iterable[PythonInfo]: """Return an iterable of interpreter paths that matches the given specifier, which can be: 1. a version specifier like 3.7 2. an absolute path 3. a short name like python3 4. None that returns all possible interpreters """ config = self.config python: str | Path | None = None if not python_spec: if config.get("python.use_pyenv", True) and PYENV_INSTALLED: pyenv_shim = os.path.join(PYENV_ROOT, "shims", "python3") if os.path.exists(pyenv_shim): yield PythonInfo.from_path(pyenv_shim) elif os.path.exists(pyenv_shim[:-1]): yield PythonInfo.from_path(pyenv_shim[:-1]) if config.get("use_venv"): python = get_in_project_venv_python(self.root) if python: yield PythonInfo.from_path(python) python = shutil.which("python") if python: yield PythonInfo.from_path(python) args = [] else: if not all(c.isdigit() for c in python_spec.split(".")): if Path(python_spec).exists(): python = find_python_in_path(python_spec) if python: yield PythonInfo.from_path(python) else: python = shutil.which(python_spec) if python: yield PythonInfo.from_path(python) return args = [int(v) for v in python_spec.split(".") if v != ""] finder = Finder() for entry in finder.find_all_python_versions(*args): yield PythonInfo.from_python_version(entry.py_version) if not python_spec: this_python = getattr(sys, "_base_executable", sys.executable) yield PythonInfo.from_path(this_python)