Better way of locating the Python 2.6/2.7 bin

It appears we can't trust sys.executable on some Unix machines as well.

Fixes #607.
This commit is contained in:
Strahinja Val Markovic 2013-10-24 20:21:03 -07:00
parent 4c88ea5249
commit 2a42e2ccdf

View File

@ -24,6 +24,7 @@ import signal
import functools import functools
import socket import socket
import stat import stat
from distutils.spawn import find_executable
WIN_PYTHON27_PATH = 'C:\python27\pythonw.exe' WIN_PYTHON27_PATH = 'C:\python27\pythonw.exe'
WIN_PYTHON26_PATH = 'C:\python26\pythonw.exe' WIN_PYTHON26_PATH = 'C:\python26\pythonw.exe'
@ -71,23 +72,31 @@ def GetUnusedLocalhostPort():
def PathToPythonInterpreter(): def PathToPythonInterpreter():
# This is a bit tricky. Normally, sys.executable has the full path to the # We check for 'pythonw' first because that covers the Windows use case (and
# Python interpreter. But this code is also executed from inside Vim's # 'pythonw' doesn't pop-up a console window like running 'python' does).
# embedded Python. On Unix machines, even that Python returns a good value for # We check for 'python2' before 'python' because some OS's (I'm looking at you
# sys.executable, but on Windows it returns the path to the Vim binary, which # Arch Linux) have made the... interesting decision to point /usr/bin/python
# is useless to us (issue #581). So we check the common install location for # to python3.
# Python on Windows, first for Python 2.7 and then for 2.6. path_to_python = FindPathToFirstExecutable(
# [ 'pythonw', 'python2', 'python' ] )
# I'm open to better ideas on how to do this. if not path_to_python:
# On Windows, Python may not be on the PATH at all, so we check some common
# install locations.
if OnWindows(): if OnWindows():
if os.path.exists( WIN_PYTHON27_PATH ): if os.path.exists( WIN_PYTHON27_PATH ):
return WIN_PYTHON27_PATH return WIN_PYTHON27_PATH
elif os.path.exists( WIN_PYTHON26_PATH ): elif os.path.exists( WIN_PYTHON26_PATH ):
return WIN_PYTHON26_PATH return WIN_PYTHON26_PATH
raise RuntimeError( 'Python 2.7/2.6 not installed!' ) raise RuntimeError( 'Python 2.7/2.6 not installed!' )
else: return path_to_python
return sys.executable
def FindPathToFirstExecutable( executable_name_list ):
for executable_name in executable_name_list:
path = find_executable( executable_name )
if path:
return path
return None
def OnWindows(): def OnWindows():