Skip to content

Commit 71a6d09

Browse files
authored
Make create_pip_script work with uv actually this time (#185)
Weird hack to work around: astral-sh/python-build-standalone#380 If we resolve the symlink all the way, the python-host interpreter works but won't install into our pyodide venv. If we don't resolve the symlink, sys.prefix is calculated incorrectly. To ensure that we get the right sys.prefix, we explicitly set it with the PYTHONHOME environment variable and then call the symlink.
1 parent eee955e commit 71a6d09

File tree

1 file changed

+24
-5
lines changed

1 file changed

+24
-5
lines changed

pyodide_build/out_of_tree/venv.py

+24-5
Original file line numberDiff line numberDiff line change
@@ -222,26 +222,45 @@ def create_pip_script(venv_bin):
222222
# Python in the shebang. Use whichever Python was used to invoke
223223
# pyodide venv.
224224
host_python_path = venv_bin / f"python{get_pyversion()}-host"
225+
host_python_path_no_version = venv_bin / "python-host"
225226
pip_path = venv_bin / "pip_patched"
227+
python_host_link = venv_bin / "python-host-link"
226228

227229
# To support the "--clear" and "--no-clear" args, we need to remove
228230
# the existing symlinks before creating new ones.
229231
host_python_path.unlink(missing_ok=True)
230-
(venv_bin / "python-host").unlink(missing_ok=True)
232+
host_python_path_no_version.unlink(missing_ok=True)
233+
python_host_link.unlink(missing_ok=True)
231234
for pip in venv_bin.glob("pip*"):
232235
if pip == pip_path:
233236
continue
234237
pip.unlink(missing_ok=True)
235238
pip.symlink_to(pip_path)
236239

237-
host_python_path.symlink_to(sys.executable)
238-
# in case someone needs a Python-version-agnostic way to refer to python-host
239-
(venv_bin / "python-host").symlink_to(sys.executable)
240+
# Weird hack to work around:
241+
# https://github.com/astral-sh/python-build-standalone/issues/380
242+
# If we resolve the symlink all the way, the python-host interpreter works
243+
# but won't install into our pyodide venv. If we don't resolve the symlink,
244+
# sys.prefix is calculated incorrectly. To ensure that we get the right
245+
# sys.prefix, we explicitly set it with the PYTHONHOME environment variable
246+
# and then call the symlink.
247+
python_host_link.symlink_to(sys.executable)
248+
pythonhome = Path(sys._base_executable).parents[1]
249+
host_python_path.write_text(
250+
dedent(
251+
f"""\
252+
#!/bin/sh
253+
exec env PYTHONHOME={pythonhome} {python_host_link} $@
254+
"""
255+
)
256+
)
257+
host_python_path.chmod(0o777)
258+
host_python_path_no_version.symlink_to(host_python_path)
240259

241260
pip_path.write_text(
242261
# Other than the shebang and the monkey patch, this is exactly what
243262
# normal pip looks like.
244-
f"#!{host_python_path} -s\n"
263+
f"#!/usr/bin/env -S {host_python_path} -s\n"
245264
+ get_pip_monkeypatch(venv_bin)
246265
+ dedent(
247266
"""

0 commit comments

Comments
 (0)