Skip to content

Commit 3bcb172

Browse files
pokebnoordhuis
authored andcommitted
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: #894 Reviewed-By: Ben Noordhuis <[email protected]>
1 parent 1dcf356 commit 3bcb172

File tree

1 file changed

+26
-1
lines changed

1 file changed

+26
-1
lines changed

lib/configure.js

+26-1
Original file line numberDiff line numberDiff line change
@@ -325,7 +325,7 @@ function findPython (python, callback) {
325325
return checkPython()
326326
}
327327
if (win) {
328-
guessPython()
328+
checkPythonLauncher()
329329
} else {
330330
failNoPython()
331331
}
@@ -340,6 +340,31 @@ function findPython (python, callback) {
340340
})
341341
}
342342

343+
// Distributions of Python on Windows by default install with the "py.exe"
344+
// Python launcher which is more likely to exist than the Python executable
345+
// being in the $PATH.
346+
// Because the Python launcher supports all versions of Python, we have to
347+
// explicitly request a Python 2 version. This is done by supplying "-2" as
348+
// the first command line argument. Since "py.exe -2" would be an invalid
349+
// executable for "execFile", we have to use the launcher to figure out
350+
// where the actual "python.exe" executable is located.
351+
function checkPythonLauncher () {
352+
log.verbose('could not find "' + python + '". checking python launcher')
353+
var env = extend({}, process.env)
354+
env.TERM = 'dumb'
355+
356+
var launcherArgs = ['-2', '-c', 'import sys; print sys.executable']
357+
execFile('py.exe', launcherArgs, { env: env }, function (err, stdout) {
358+
if (err) {
359+
guessPython()
360+
return
361+
}
362+
python = stdout.trim()
363+
log.verbose('check python launcher', 'python executable found: %j', python)
364+
checkPythonVersion()
365+
})
366+
}
367+
343368
// Called on Windows when "python" isn't available in the current $PATH.
344369
// We're gonna check if "%SystemDrive%\python27\python.exe" exists.
345370
function guessPython () {

0 commit comments

Comments
 (0)