Skip to content

Instantly share code, notes, and snippets.

@njsmith
Last active November 10, 2015 01:16
Show Gist options
  • Select an option

  • Save njsmith/c051a8298cc641bcfef4 to your computer and use it in GitHub Desktop.

Select an option

Save njsmith/c051a8298cc641bcfef4 to your computer and use it in GitHub Desktop.
import sys
import os
import os.path
import subprocess
import json
# entry point for 'python -m pip'
def python_m_pip_main():
do_pip(sys.argv)
# entry point for 'pip ...'
def pip_main():
check_pip_python_consistency()
do_pip(sys.argv)
# assumes POSIX for now
# needs to do something else on windows, but I don't know what :-)
def check_pip_python_consistency():
pip_path = sys.argv[0]
dirname, basename = os.path.split(pip_path)
if not basename.startswith("pip"):
# WTF?
# I guess we continue and hope for the best...
return
python_basename = "python" + basename[len("pip"):]
python_path = os.path.join(dirname, python_basename)
# There's no reliable way to map from python executable path to environment
# except by running it. This also lets us skip trying to implement our own
# $PATH lookup...
try:
# mumble mumble do something about str/bytes here
result = subprocess.check_output(
[python_path, "-c", "import sys, json; json.dump([sys.prefix, sys.exec_prefix], sys.stdout)"])
except subprocess.CalledProcessError:
# Something is horribly wrong
# But we have no idea what, so I guess we continue and hope for the best?
return
if json.loads(result) != [sys.prefix, sys.exec_prefix]:
sys.stderr.write(
"Mismatch detected between 'pip' and 'python' commands.\n"
"This usually indicates some sort of installation problem.\n"
"You ran:\n"
" {pip_path}\n"
"which executed using the python interpreter at:\n"
" {pip_python_path}\n"
"and we found a *different* python interpreter at:\n"
" {python_path}\n"
"If you wanted to manage the {python_path} environment, try running:\n"
" {python_path} -m pip <arguments>\n"
"If you really did want to manage the {pip_python_path} environment,\n"
"adding --force-no-interpreter-check to your command-line will disable\n"
"this check.\n"
.format(pip_path=pip_path,
pip_python_path=sys.executable,
python_path=python_path))
sys.exit(1)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment