Skip to content

Commit

Permalink
Allow pip install --executable=/path to set #! path in scripts.
Browse files Browse the repository at this point in the history
This is useful for cross-compiling, when the path to the Python
executable that is being used to run pip, available at
sys.executable, is not the same as the path to the Python executable
that the installed product in a cross-compiled system needs to run.

For example, if your toolchain lives at /build/aarch64--netbsd but
you're building software to be installed at /opt/pkg, then
sys.executable might be /build/aarch64--netbsd/bin/python3.10 but the
shebang line of any scripts installed must /opt/pkg/bin/python3.10.
With this change, you can accomplish that with:

/build/aarch64--netbsd/bin/python3.10 -m pip install --executable=/opt/pkg/bin/python3.10 ...

fix #12087
  • Loading branch information
Taylor R Campbell committed Jun 14, 2023
1 parent 8a1eea4 commit 9027fbd
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 1 deletion.
9 changes: 9 additions & 0 deletions src/pip/_internal/commands/install.py
Expand Up @@ -141,6 +141,14 @@ def add_options(self) -> None:
"environment."
),
)
self.cmd_opts.add_option(
"--executable",
dest="executable_path",
default=None,
help=(
"Path to executable for #! lines in scripts."
),
)

self.cmd_opts.add_option(cmdoptions.src())

Expand Down Expand Up @@ -452,6 +460,7 @@ def run(self, options: Values, args: List[str]) -> int:
root=options.root_path,
home=target_temp_dir_path,
prefix=options.prefix_path,
executable=options.executable_path,
warn_script_location=warn_script_location,
use_user_site=options.use_user_site,
pycompile=options.compile,
Expand Down
8 changes: 7 additions & 1 deletion src/pip/_internal/operations/install/wheel.py
Expand Up @@ -422,6 +422,9 @@ def _raise_for_invalid_entrypoint(specification: str) -> None:


class PipScriptMaker(ScriptMaker):
def __init__(self, executable, *args, **kwargs):
super().__init__(*args, **kwargs)
self.executable = executable
def make(
self, specification: str, options: Optional[Dict[str, Any]] = None
) -> List[str]:
Expand All @@ -434,6 +437,7 @@ def _install_wheel(
wheel_zip: ZipFile,
wheel_path: str,
scheme: Scheme,
executable: Optional[str] = None,
pycompile: bool = True,
warn_script_location: bool = True,
direct_url: Optional[DirectUrl] = None,
Expand Down Expand Up @@ -624,7 +628,7 @@ def pyc_output_path(path: str) -> str:
record_installed(pyc_record_path, pyc_path)
logger.debug(stdout.getvalue())

maker = PipScriptMaker(None, scheme.scripts)
maker = PipScriptMaker(executable, None, scheme.scripts)

# Ensure old scripts are overwritten.
# See https://github.com/pypa/pip/issues/1800
Expand Down Expand Up @@ -721,6 +725,7 @@ def install_wheel(
wheel_path: str,
scheme: Scheme,
req_description: str,
executable: Optional[str] = None,
pycompile: bool = True,
warn_script_location: bool = True,
direct_url: Optional[DirectUrl] = None,
Expand All @@ -733,6 +738,7 @@ def install_wheel(
wheel_zip=z,
wheel_path=wheel_path,
scheme=scheme,
executable=executable,
pycompile=pycompile,
warn_script_location=warn_script_location,
direct_url=direct_url,
Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/req/__init__.py
Expand Up @@ -40,6 +40,7 @@ def install_given_reqs(
root: Optional[str],
home: Optional[str],
prefix: Optional[str],
executable: Optional[str],
warn_script_location: bool,
use_user_site: bool,
pycompile: bool,
Expand Down Expand Up @@ -74,6 +75,7 @@ def install_given_reqs(
root=root,
home=home,
prefix=prefix,
executable=executable,
warn_script_location=warn_script_location,
use_user_site=use_user_site,
pycompile=pycompile,
Expand Down
2 changes: 2 additions & 0 deletions src/pip/_internal/req/req_install.py
Expand Up @@ -773,6 +773,7 @@ def install(
root: Optional[str] = None,
home: Optional[str] = None,
prefix: Optional[str] = None,
executable: Optional[str] = None,
warn_script_location: bool = True,
use_user_site: bool = False,
pycompile: bool = True,
Expand Down Expand Up @@ -808,6 +809,7 @@ def install(
self.name,
self.local_file_path,
scheme=scheme,
executable=executable,
req_description=str(self.req),
pycompile=pycompile,
warn_script_location=warn_script_location,
Expand Down

0 comments on commit 9027fbd

Please sign in to comment.