Locate only the current Python version's site-packages in nearby .venv dirs.

This commit is contained in:
2026-08-13 21:27:14 +00:00
parent 20694576c7
commit 955fdd8e1c
2 changed files with 27 additions and 6 deletions
+5 -3
View File
@@ -199,15 +199,17 @@ def _format_size(n: int) -> str:
def _find_venv_site_packages(start: Path) -> list[Path]: def _find_venv_site_packages(start: Path) -> list[Path]:
"""Return site-packages dirs of ``.venv`` directories from *start* to parents.""" """Return site-packages dirs of ``.venv`` directories from *start* to parents."""
py_dir = f"python{sys.version_info.major}.{sys.version_info.minor}"
found: list[Path] = [] found: list[Path] = []
for parent in [start, *start.parents]: for parent in [start, *start.parents]:
venv = parent / ".venv" venv = parent / ".venv"
if not venv.is_dir(): if not venv.is_dir():
continue continue
for site_packages in venv.glob("lib/python*/site-packages"): site_packages = venv / "lib" / py_dir / "site-packages"
if site_packages.is_dir():
found.append(site_packages) found.append(site_packages)
break continue
else: # Windows layout
win_site = venv / "Lib" / "site-packages" win_site = venv / "Lib" / "site-packages"
if win_site.is_dir(): if win_site.is_dir():
found.append(win_site) found.append(win_site)
+19
View File
@@ -51,6 +51,25 @@ def test_extra_import_paths_are_temporary(tmp_path, monkeypatch):
assert sys.path == before assert sys.path == before
def test_extra_import_paths_ignores_other_python_versions(tmp_path, monkeypatch):
"""Only the site-packages for the running Python version is picked up."""
current_site = (
tmp_path
/ ".venv"
/ "lib"
/ f"python{sys.version_info.major}.{sys.version_info.minor}"
/ "site-packages"
)
other_site = tmp_path / ".venv" / "lib" / "python9.9" / "site-packages"
current_site.mkdir(parents=True)
other_site.mkdir(parents=True)
monkeypatch.chdir(tmp_path)
with _extra_import_paths():
assert str(current_site) in sys.path
assert str(other_site) not in sys.path
def test_cli_snapshot_line_format(tmp_path, capsys): def test_cli_snapshot_line_format(tmp_path, capsys):
"""Snapshot lines are timestamped and colored with metadata.""" """Snapshot lines are timestamped and colored with metadata."""
path = tmp_path / "test.kantadb" path = tmp_path / "test.kantadb"