Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
31 changes: 21 additions & 10 deletions src/ducktools/pythonfinder/shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,11 @@

from . import details_script

if sys.version_info < (3, 15):
import types
frozendict = types.MappingProxyType


_laz = LazyImporter(
[
FromImport("glob", "glob"),
Expand Down Expand Up @@ -337,6 +342,7 @@ class PythonInstall(Prefab):
shadowed: bool = attribute(default=False, serialize=False)
_implementation_version: tuple[int, int, int, str, int] | None = attribute(default=None, private=True)
_real_executable: str | None = attribute(default=None, private=True)
_pip_version_str: str | None = attribute(default=None, private=True)

def __prefab_post_init__(
self,
Expand Down Expand Up @@ -469,22 +475,27 @@ def from_json(

def get_pip_version(self) -> str | None:
"""
Get the version of pip installed on a python install.
Get the version of ensurepip installed on a python install.

This is the version of pip that will be used to create venvs

:return: None if pip is not found or the command fails
version number as string otherwise.
"""
pip_call = _laz.subprocess.run(
[self.executable, "-c", "import pip; print(pip.__version__, end='')"],
text=True,
capture_output=True,
)
if self._pip_version_str is None:
pip_call = _laz.subprocess.run(
[self.executable, "-c", "import ensurepip; print(ensurepip.version(), end='')"],
text=True,
capture_output=True,
)

# Pip call failed
if pip_call.returncode != 0:
return None
# Pip call failed
if pip_call.returncode != 0:
return None

self._pip_version_str = pip_call.stdout

return pip_call.stdout
return self._pip_version_str


# Return type missing due to import requirements
Expand Down
4 changes: 2 additions & 2 deletions tests/test_shared.py
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ def test_pip_version():
pip_ver = inst.get_pip_version()

mock_run.assert_called_once_with(
[sys.executable, "-c", "import pip; print(pip.__version__, end='')"],
[sys.executable, "-c", "import ensurepip; print(ensurepip.version(), end='')"],
text=True,
capture_output=True,
)
Expand All @@ -116,7 +116,7 @@ def test_fail_pip_version():
pip_ver = inst.get_pip_version()

mock_run.assert_called_once_with(
[sys.executable, "-c", "import pip; print(pip.__version__, end='')"],
[sys.executable, "-c", "import ensurepip; print(ensurepip.version(), end='')"],
text=True,
capture_output=True,
)
Expand Down
Loading
Loading