From 78b3f0063ad9f25cac49726006fdc61011f52af4 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Wed, 9 Apr 2025 20:48:20 +0200 Subject: [PATCH 1/6] Make create_pip_script work with uv actually this time [integration] Weird hack to work around: https://github.com/astral-sh/python-build-standalone/issues/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. --- pyodide_build/out_of_tree/venv.py | 25 +++++++++++++++++++++---- 1 file changed, 21 insertions(+), 4 deletions(-) diff --git a/pyodide_build/out_of_tree/venv.py b/pyodide_build/out_of_tree/venv.py index 2dae8f71..5aaa0709 100644 --- a/pyodide_build/out_of_tree/venv.py +++ b/pyodide_build/out_of_tree/venv.py @@ -223,20 +223,37 @@ def create_pip_script(venv_bin): # pyodide venv. host_python_path = venv_bin / f"python{get_pyversion()}-host" pip_path = venv_bin / "pip_patched" + python_host_link = venv_bin / "python-host-link" # To support the "--clear" and "--no-clear" args, we need to remove # the existing symlinks before creating new ones. host_python_path.unlink(missing_ok=True) - (venv_bin / "python-host").unlink(missing_ok=True) + python_host_link.unlink(missing_ok=True) for pip in venv_bin.glob("pip*"): if pip == pip_path: continue pip.unlink(missing_ok=True) pip.symlink_to(pip_path) - host_python_path.symlink_to(sys.executable) - # in case someone needs a Python-version-agnostic way to refer to python-host - (venv_bin / "python-host").symlink_to(sys.executable) + # Weird hack to work around: + # https://github.com/astral-sh/python-build-standalone/issues/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. + python_host_link.symlink_to(sys.executable) + pythonhome = Path(sys._base_executable).parents[1] + host_python_path.write_text( + dedent( + f"""\ + #!/bin/sh + exec env PYTHONHOME={pythonhome} {sys.executable} $@ + """ + ) + ) + host_python_path.chmod(0o777) + (venv_bin / "python-host").symlink_to(host_python_path) pip_path.write_text( # Other than the shebang and the monkey patch, this is exactly what From e75de3211159f8d623d2a3dd79dc21e8112580b4 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Fri, 11 Apr 2025 11:22:11 +0200 Subject: [PATCH 2/6] Fix --- pyodide_build/out_of_tree/venv.py | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/pyodide_build/out_of_tree/venv.py b/pyodide_build/out_of_tree/venv.py index 5aaa0709..ad400bc0 100644 --- a/pyodide_build/out_of_tree/venv.py +++ b/pyodide_build/out_of_tree/venv.py @@ -222,12 +222,14 @@ def create_pip_script(venv_bin): # Python in the shebang. Use whichever Python was used to invoke # pyodide venv. host_python_path = venv_bin / f"python{get_pyversion()}-host" + host_python_path_no_version = venv_bin / f"python-host" pip_path = venv_bin / "pip_patched" python_host_link = venv_bin / "python-host-link" # To support the "--clear" and "--no-clear" args, we need to remove # the existing symlinks before creating new ones. host_python_path.unlink(missing_ok=True) + host_python_path_no_version.unlink(missing_ok=True) python_host_link.unlink(missing_ok=True) for pip in venv_bin.glob("pip*"): if pip == pip_path: @@ -253,7 +255,7 @@ def create_pip_script(venv_bin): ) ) host_python_path.chmod(0o777) - (venv_bin / "python-host").symlink_to(host_python_path) + host_python_path_no_version.symlink_to(host_python_path) pip_path.write_text( # Other than the shebang and the monkey patch, this is exactly what From acd10d625aa0a41a040319b7c3b3780487dd4c4d Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Fri, 11 Apr 2025 09:22:45 +0000 Subject: [PATCH 3/6] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- pyodide_build/out_of_tree/venv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyodide_build/out_of_tree/venv.py b/pyodide_build/out_of_tree/venv.py index ad400bc0..36d32d8d 100644 --- a/pyodide_build/out_of_tree/venv.py +++ b/pyodide_build/out_of_tree/venv.py @@ -222,7 +222,7 @@ def create_pip_script(venv_bin): # Python in the shebang. Use whichever Python was used to invoke # pyodide venv. host_python_path = venv_bin / f"python{get_pyversion()}-host" - host_python_path_no_version = venv_bin / f"python-host" + host_python_path_no_version = venv_bin / "python-host" pip_path = venv_bin / "pip_patched" python_host_link = venv_bin / "python-host-link" From d6f3937265e705df3a7df5b4b08722c493ef9c85 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Tue, 15 Apr 2025 13:55:21 -0400 Subject: [PATCH 4/6] Update shebang to use /usr/bin/env --- pyodide_build/out_of_tree/venv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyodide_build/out_of_tree/venv.py b/pyodide_build/out_of_tree/venv.py index 36d32d8d..0ae1aaac 100644 --- a/pyodide_build/out_of_tree/venv.py +++ b/pyodide_build/out_of_tree/venv.py @@ -260,7 +260,7 @@ def create_pip_script(venv_bin): pip_path.write_text( # Other than the shebang and the monkey patch, this is exactly what # normal pip looks like. - f"#!{host_python_path} -s\n" + f"#!/usr/bin/env {host_python_path} -s\n" + get_pip_monkeypatch(venv_bin) + dedent( """ From 06f79ac26fc70a6c03f1144141fea2e7ee913103 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Tue, 15 Apr 2025 14:06:19 -0400 Subject: [PATCH 5/6] env -s --- pyodide_build/out_of_tree/venv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyodide_build/out_of_tree/venv.py b/pyodide_build/out_of_tree/venv.py index 0ae1aaac..509c930a 100644 --- a/pyodide_build/out_of_tree/venv.py +++ b/pyodide_build/out_of_tree/venv.py @@ -260,7 +260,7 @@ def create_pip_script(venv_bin): pip_path.write_text( # Other than the shebang and the monkey patch, this is exactly what # normal pip looks like. - f"#!/usr/bin/env {host_python_path} -s\n" + f"#!/usr/bin/env -S {host_python_path} -s\n" + get_pip_monkeypatch(venv_bin) + dedent( """ From e78ba64978b2fa374fc54ed4fc8213df75d85498 Mon Sep 17 00:00:00 2001 From: Hood Chatham Date: Tue, 15 Apr 2025 14:10:34 -0400 Subject: [PATCH 6/6] Use correct executable path --- pyodide_build/out_of_tree/venv.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pyodide_build/out_of_tree/venv.py b/pyodide_build/out_of_tree/venv.py index 509c930a..2e9e3eb1 100644 --- a/pyodide_build/out_of_tree/venv.py +++ b/pyodide_build/out_of_tree/venv.py @@ -250,7 +250,7 @@ def create_pip_script(venv_bin): dedent( f"""\ #!/bin/sh - exec env PYTHONHOME={pythonhome} {sys.executable} $@ + exec env PYTHONHOME={pythonhome} {python_host_link} $@ """ ) )