diff --git a/docs/project/changelog.md b/docs/project/changelog.md index 80b2fa8fd..a6bc1e66f 100644 --- a/docs/project/changelog.md +++ b/docs/project/changelog.md @@ -43,6 +43,9 @@ myst: version 0.23.0 because of missing `python_stdlib.zip`. {pr}`3760` +- {{ Fix }} `python -m pip` works correctly in the Pyodide venv now. + {pr}`3761` + ### Build System - {{ Fix }} Fix `PYODIDE_ROOT` to point the correct directory when running out-of-tree build. diff --git a/src/templates/python b/src/templates/python index 752002dba..5de597fd9 100755 --- a/src/templates/python +++ b/src/templates/python @@ -1,4 +1,4 @@ -#!/bin/sh +#!/bin/bash ":" /* << "EOF" This file is a bash/node polyglot. This is needed for a few reasons: @@ -21,6 +21,11 @@ inside the virtualenv the proper shell code is executed. EOF # bash set -e +if [[ $1 == "-m" ]] && [[ $2 == "pip" ]]; then + # redirect python -m pip to execute in host environment + shift 1 + exec $@ +fi which node > /dev/null || { \ >&2 echo "No node executable found on the path" && exit 1; \ diff --git a/src/tests/test_cmdline_runner.py b/src/tests/test_cmdline_runner.py index b524fd87d..964815b72 100644 --- a/src/tests/test_cmdline_runner.py +++ b/src/tests/test_cmdline_runner.py @@ -111,6 +111,25 @@ def test_dash_m(selenium): assert result.stdout.strip() == f"Emscripten-{emscripten_version()}-wasm32-32bit" +@only_node +def test_dash_m_pip(selenium, monkeypatch, tmp_path): + import os + + monkeypatch.setenv("PATH", str(tmp_path), prepend=":") + pip_path = tmp_path / "pip" + pip_path.write_text("echo 'pip got' $@") + os.chmod(pip_path, 0o777) + + result = subprocess.run( + [script_path, "-m", "pip", "install", "pytest"], + capture_output=True, + encoding="utf8", + ) + assert result.returncode == 0 + assert result.stderr == "" + assert result.stdout.strip() == "pip got install pytest" + + @only_node def test_invalid_cmdline_option(selenium): result = subprocess.run([script_path, "-c"], capture_output=True, encoding="utf8")