diff --git a/kanta/__main__.py b/kanta/__main__.py index f715b83..b461b87 100644 --- a/kanta/__main__.py +++ b/kanta/__main__.py @@ -199,18 +199,20 @@ def _format_size(n: int) -> str: def _find_venv_site_packages(start: Path) -> list[Path]: """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] = [] for parent in [start, *start.parents]: venv = parent / ".venv" if not venv.is_dir(): 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) - break - else: - win_site = venv / "Lib" / "site-packages" - if win_site.is_dir(): - found.append(win_site) + continue + # Windows layout + win_site = venv / "Lib" / "site-packages" + if win_site.is_dir(): + found.append(win_site) return found diff --git a/tests/test_cli.py b/tests/test_cli.py index 54c133e..405f182 100644 --- a/tests/test_cli.py +++ b/tests/test_cli.py @@ -51,6 +51,25 @@ def test_extra_import_paths_are_temporary(tmp_path, monkeypatch): 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): """Snapshot lines are timestamped and colored with metadata.""" path = tmp_path / "test.kantadb"