Implement new strategy to find Python path
This commit is contained in:
parent
135923a40a
commit
c7e32b3600
@ -18,14 +18,14 @@
|
|||||||
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
# along with YouCompleteMe. If not, see <http://www.gnu.org/licenses/>.
|
||||||
|
|
||||||
import os
|
import os
|
||||||
|
import sys
|
||||||
import vim
|
import vim
|
||||||
import functools
|
import functools
|
||||||
from ycmd import utils
|
from ycmd import utils
|
||||||
|
|
||||||
DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
|
DIR_OF_CURRENT_SCRIPT = os.path.dirname( os.path.abspath( __file__ ) )
|
||||||
|
|
||||||
WIN_PYTHON27_PATH = 'C:\python27\python.exe'
|
WIN_PYTHON_PATH = os.path.join( sys.exec_prefix, 'python.exe' )
|
||||||
WIN_PYTHON26_PATH = 'C:\python26\python.exe'
|
|
||||||
|
|
||||||
|
|
||||||
def Memoize( obj ):
|
def Memoize( obj ):
|
||||||
@ -42,29 +42,39 @@ def Memoize( obj ):
|
|||||||
|
|
||||||
@Memoize
|
@Memoize
|
||||||
def PathToPythonInterpreter():
|
def PathToPythonInterpreter():
|
||||||
user_path_to_python = vim.eval( 'g:ycm_path_to_python_interpreter' )
|
python_interpreter = vim.eval( 'g:ycm_path_to_python_interpreter' )
|
||||||
|
|
||||||
if user_path_to_python:
|
if python_interpreter:
|
||||||
return user_path_to_python
|
if not CheckPythonVersion( python_interpreter ):
|
||||||
|
raise RuntimeError( "Path in 'g:ycm_path_to_python_interpreter' option "
|
||||||
|
"does not point to a valid Python 2.6 or 2.7." )
|
||||||
|
|
||||||
# We check for 'python2' before 'python' because some OS's (I'm looking at
|
return python_interpreter
|
||||||
# you Arch Linux) have made the... interesting decision to point
|
|
||||||
# /usr/bin/python to python3.
|
|
||||||
python_names = [ 'python2', 'python' ]
|
|
||||||
|
|
||||||
path_to_python = utils.PathToFirstExistingExecutable( python_names )
|
# On UNIX platforms, we use sys.executable as the Python interpreter path.
|
||||||
if path_to_python:
|
# We cannot use sys.executable on Windows because for unknown reasons, it
|
||||||
return path_to_python
|
# returns the Vim executable. Instead, we use sys.exec_prefix to deduce the
|
||||||
|
# interpreter path.
|
||||||
|
python_interpreter = ( WIN_PYTHON_PATH if utils.OnWindows() else
|
||||||
|
sys.executable )
|
||||||
|
|
||||||
# On Windows, Python may not be on the PATH at all, so we check some common
|
if not CheckPythonVersion( python_interpreter ):
|
||||||
# install locations.
|
raise RuntimeError( "Cannot find Python 2.6 or 2.7. You can set its path "
|
||||||
if utils.OnWindows():
|
"using the 'g:ycm_path_to_python_interpreter' "
|
||||||
if os.path.exists( WIN_PYTHON27_PATH ):
|
"option." )
|
||||||
return WIN_PYTHON27_PATH
|
|
||||||
elif os.path.exists( WIN_PYTHON26_PATH ):
|
|
||||||
return WIN_PYTHON26_PATH
|
|
||||||
|
|
||||||
raise RuntimeError( 'Python 2.7/2.6 not installed!' )
|
return python_interpreter
|
||||||
|
|
||||||
|
|
||||||
|
def CheckPythonVersion( python_interpreter ):
|
||||||
|
"""Check if given path is the Python interpreter version 2.6 or 2.7."""
|
||||||
|
command = [ python_interpreter,
|
||||||
|
'-c',
|
||||||
|
"import sys;"
|
||||||
|
"major, minor = sys.version_info[ :2 ];"
|
||||||
|
"sys.exit( major != 2 or minor < 6)" ]
|
||||||
|
|
||||||
|
return utils.SafePopen( command ).wait() == 0
|
||||||
|
|
||||||
|
|
||||||
def PathToServerScript():
|
def PathToServerScript():
|
||||||
|
Loading…
x
Reference in New Issue
Block a user