There’s a common piece of advice to run python -m pip
rather than just pip
to avoid various problems, in particular the issue whereby python
finds a different Python installation than pip
does. However, the Pip scripts are all written with the idea that pip
on the command line should “just work” (upgrading Pip with itself on Windows notwithstanding). For example, in the Linux implementation, the shebang lines for the wrapper scripts specify the corresponding Python explicitly. Further, people will try it anyway, as long as it’s at all advertised that it could work at all.
Assuming “deprecate and remove the ability to use pip
as such at the command line” is not an option, I propose that Pip should be able to detect the situation where it is running a different Python, vs. the one that python
would invoke in the current environment, and issue a warning and a [y/N]
prompt to continue. It could do this on current versions of Python by something like:
from pathlib import Path
from subprocess import run
import sys
def warn_python(xdesc, xpath, ydesc, ypath):
ok = input(f'{xdesc} (at {xpath}) does not match {ydesc} (at {ypath}). Continue? [y/N]')
if ok != 'y':
sys.exit(1)
here = Path('__file__').parent.resolve()
running_python = Path(sys.executable)
# This needs to be adjusted depending on the OS
corresponding_python = here / 'python'
# This might also need adjustment to account for `pythonx.y` commands?
found_python = run(('which', 'python'), capture_output=True).stdout.decode().strip()
if found_python != running_python:
warn(
'Python found by the `python` command', found_python,
'Python currently being used by Pip', running_python
)
elif found_python != corresponding_python:
warn(
'Python found by the `python` command', found_python,
'Python installation that contains this Pip script', corresponding_python
)