path.py 662 B

12345678910111213141516171819202122232425
  1. from __future__ import annotations
  2. import os
  3. import typing as t
  4. from dataclasses import dataclass
  5. from pathlib import Path
  6. from findpython.providers.base import BaseProvider
  7. from findpython.python import PythonVersion
  8. @dataclass
  9. class PathProvider(BaseProvider):
  10. """A provider that finds Python from PATH env."""
  11. paths: list[Path]
  12. @classmethod
  13. def create(cls) -> t.Self | None:
  14. paths = [Path(path) for path in os.getenv("PATH", "").split(os.pathsep) if path]
  15. return cls(paths)
  16. def find_pythons(self) -> t.Iterable[PythonVersion]:
  17. for path in self.paths:
  18. yield from self.find_pythons_from_path(path)