Skip to content

Commit d37bff9

Browse files
authored
fix pip tags to use resolved python from environment (#2608)
1 parent e6f9775 commit d37bff9

1 file changed

Lines changed: 29 additions & 3 deletions

File tree

metaflow/plugins/pypi/pip.py

Lines changed: 29 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,20 +60,35 @@ def __init__(self, micromamba=None, logger=None):
6060
else:
6161
self.logger = lambda *args, **kwargs: None # No-op logger if not provided
6262

63+
def _get_resolved_python_version(self, prefix):
64+
try:
65+
result = self.micromamba._call(["list", "--prefix", prefix, "--json"])
66+
for package in result:
67+
if package.get("name") == "python":
68+
return package["version"]
69+
except Exception:
70+
return None
71+
6372
def solve(self, id_, packages, python, platform):
6473
prefix = self.micromamba.path_to_environment(id_)
6574
if prefix is None:
6675
msg = "Unable to locate a Micromamba managed virtual environment\n"
6776
msg += "for id {id}".format(id=id_)
6877
raise PipException(msg)
6978

79+
resolved_python = self._get_resolved_python_version(prefix)
80+
if not resolved_python:
81+
raise PipException(
82+
"Could not determine Python version from conda environment"
83+
)
84+
7085
debug.conda_exec("Solving packages for PyPI environment %s" % id_)
7186
with tempfile.TemporaryDirectory() as tmp_dir:
7287
report = "{tmp_dir}/report.json".format(tmp_dir=tmp_dir)
7388
implementations, platforms, abis = zip(
7489
*[
7590
(tag.interpreter, tag.platform, tag.abi)
76-
for tag in pip_tags(python, platform)
91+
for tag in pip_tags(resolved_python, platform)
7792
]
7893
)
7994
custom_index_url, extra_index_urls = self.indices(prefix)
@@ -142,6 +157,13 @@ def _format(dl_info):
142157

143158
def download(self, id_, packages, python, platform):
144159
prefix = self.micromamba.path_to_environment(id_)
160+
161+
resolved_python = self._get_resolved_python_version(prefix)
162+
if not resolved_python:
163+
raise PipException(
164+
"Could not determine Python version from conda environment"
165+
)
166+
145167
metadata_file = METADATA_FILE.format(prefix=prefix)
146168
# download packages only if they haven't ever been downloaded before
147169
if os.path.isfile(metadata_file):
@@ -192,7 +214,11 @@ def _build(key, package):
192214
if os.path.isfile(os.path.join(path, f)) and f.endswith(".whl")
193215
]
194216
if (
195-
len(set(pip_tags(python, platform)).intersection(wheel_tags(wheel)))
217+
len(
218+
set(pip_tags(resolved_python, platform)).intersection(
219+
wheel_tags(wheel)
220+
)
221+
)
196222
== 0
197223
):
198224
raise PipException(
@@ -211,7 +237,7 @@ def _build(key, package):
211237
implementations, platforms, abis = zip(
212238
*[
213239
(tag.interpreter, tag.platform, tag.abi)
214-
for tag in pip_tags(python, platform)
240+
for tag in pip_tags(resolved_python, platform)
215241
]
216242
)
217243

0 commit comments

Comments
 (0)