Skip to content

Commit 79cd599

Browse files
committed
Add a --python option
1 parent b8ccb04 commit 79cd599

File tree

2 files changed

+23
-0
lines changed

2 files changed

+23
-0
lines changed

src/pip/_internal/cli/cmdoptions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -189,6 +189,13 @@ class PipOption(Option):
189189
),
190190
)
191191

192+
python: Callable[..., Option] = partial(
193+
Option,
194+
"--python",
195+
dest="python",
196+
help="Run pip with the specified Python interpreter.",
197+
)
198+
192199
verbose: Callable[..., Option] = partial(
193200
Option,
194201
"-v",
@@ -1029,6 +1036,7 @@ def check_list_path_option(options: Values) -> None:
10291036
debug_mode,
10301037
isolated_mode,
10311038
require_virtualenv,
1039+
python,
10321040
verbose,
10331041
version,
10341042
quiet,

src/pip/_internal/cli/main_parser.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,11 @@
22
"""
33

44
import os
5+
import subprocess
56
import sys
67
from typing import List, Tuple
78

9+
from pip._internal.build_env import _get_runnable_pip
810
from pip._internal.cli import cmdoptions
911
from pip._internal.cli.parser import ConfigOptionParser, UpdatingDefaultsHelpFormatter
1012
from pip._internal.commands import commands_dict, get_similar_commands
@@ -57,6 +59,19 @@ def parse_command(args: List[str]) -> Tuple[str, List[str]]:
5759
# args_else: ['install', '--user', 'INITools']
5860
general_options, args_else = parser.parse_args(args)
5961

62+
# --python
63+
if general_options.python:
64+
if "_PIP_RUNNING_IN_SUBPROCESS" not in os.environ:
65+
pip_cmd = [
66+
general_options.python,
67+
_get_runnable_pip(),
68+
]
69+
pip_cmd.extend(args)
70+
# Block recursing indefinitely
71+
os.environ["_PIP_RUNNING_IN_SUBPROCESS"] = "1"
72+
proc = subprocess.run(pip_cmd)
73+
sys.exit(proc.returncode)
74+
6075
# --version
6176
if general_options.version:
6277
sys.stdout.write(parser.version)

0 commit comments

Comments
 (0)