From 3bcb1720e472a2f029bcac5c1f0e0b6757bd45b9 Mon Sep 17 00:00:00 2001 From: Patrick Westerhoff Date: Mon, 14 Mar 2016 23:26:28 +0100 Subject: [PATCH] Add support for the Python launcher on Windows When looking for a Python executable on Windows, before falling back to guessing the default location or failing completely, attempt to use the Python launcher to figure out the location of the Python executable. The Python launcher is being distributed by default with Python distributions on Windows, and is placed in the %WINDIR% folder (which is in the PATH). This allows us to locate a Python installation even if it was installed without putting the python.exe executable itself into the PATH. Because the Python launcher supports all versions of Python, we have to explicitly request a Python 2 version. This is done by supplying "-2" as the first command line argument. Since "py.exe -2" would be an invalid executable for "execFile", we have to use the launcher to figure out where the actual "python.exe" executable is located. PR-URL: https://github.com/nodejs/node-gyp/pull/894 Reviewed-By: Ben Noordhuis --- lib/configure.js | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/lib/configure.js b/lib/configure.js index 4e0652961e..22251ba725 100644 --- a/lib/configure.js +++ b/lib/configure.js @@ -325,7 +325,7 @@ function findPython (python, callback) { return checkPython() } if (win) { - guessPython() + checkPythonLauncher() } else { failNoPython() } @@ -340,6 +340,31 @@ function findPython (python, callback) { }) } + // Distributions of Python on Windows by default install with the "py.exe" + // Python launcher which is more likely to exist than the Python executable + // being in the $PATH. + // Because the Python launcher supports all versions of Python, we have to + // explicitly request a Python 2 version. This is done by supplying "-2" as + // the first command line argument. Since "py.exe -2" would be an invalid + // executable for "execFile", we have to use the launcher to figure out + // where the actual "python.exe" executable is located. + function checkPythonLauncher () { + log.verbose('could not find "' + python + '". checking python launcher') + var env = extend({}, process.env) + env.TERM = 'dumb' + + var launcherArgs = ['-2', '-c', 'import sys; print sys.executable'] + execFile('py.exe', launcherArgs, { env: env }, function (err, stdout) { + if (err) { + guessPython() + return + } + python = stdout.trim() + log.verbose('check python launcher', 'python executable found: %j', python) + checkPythonVersion() + }) + } + // Called on Windows when "python" isn't available in the current $PATH. // We're gonna check if "%SystemDrive%\python27\python.exe" exists. function guessPython () {