Skip to content

Commit

Permalink
Add support for the Python launcher on Windows
Browse files Browse the repository at this point in the history
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: #894
Reviewed-By: Ben Noordhuis <info@bnoordhuis.nl>
  • Loading branch information
poke authored and bnoordhuis committed Mar 31, 2016
1 parent 1dcf356 commit 3bcb172
Showing 1 changed file with 26 additions and 1 deletion.
27 changes: 26 additions & 1 deletion lib/configure.js
Expand Up @@ -325,7 +325,7 @@ function findPython (python, callback) {
return checkPython()
}
if (win) {
guessPython()
checkPythonLauncher()
} else {
failNoPython()
}
Expand All @@ -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 () {
Expand Down

0 comments on commit 3bcb172

Please sign in to comment.