Calling python -m pip inside python application

I am packaging an application that does a bit of environment management using pip. In essence, it does this:

import subprocess
import sys


def main():
    own_python = sys.executable
    print(own_python)
    # this is silly and not what the original application does. 
    # the point is to invoke `python -m pip` and not fail with "pip module does not exist"
    process = subprocess.run([own_python, "-m", "pip", "install", "foo"],
                   capture_output=True,
                   text=True
                   )

    print(process.returncode)
    print(process.stderr)


if __name__ == "__main__":
    main()

The application relies on the fact that python interpreter that’s invoking it has pip module available for a certain functionality. The rest of the application successfully gets all dependencies during runtime as they are passed through buildPythonApplication.

I have extracted the relevant bits into this flake runnable as:

nix run github:VTimofeenko/python-pip-nix-scratchpad

Question is: is it possible to use one of the parameters of buildPythonApplication or overrides of python to make python -m pip work inside the script?

I’d imagine it is possible to use an override of the python configure flag to set --ensurepip but that would require recompiling python. Or I could write a patch to replace subprocess call with the proper pip location but I’d rather keep the code as close to upstream as possible.

Upstream redesigned the way python was called and it just works now. So this thread becomes a purely academic question