Skip to content

Commit

Permalink
lib: try to find python after python3
Browse files Browse the repository at this point in the history
Unadorned `python` can be either Python 2 or Python 3, but it is likely
to be Python 2 for quite a while.

To find Python3, it is recommended to use the explicit name `python3`.

See:
- https://www.python.org/dev/peps/pep-0394/#for-python-runtime-distributors
- #1892 (comment)

PR-URL: #1907
Reviewed-By: Christian Clauss <cclauss@me.com>
Reviewed-By: Rod Vagg <rod@vagg.org>
Reviewed-By: João Reis <reis@janeasystems.com>
  • Loading branch information
sam-github authored and rvagg committed Oct 4, 2019
1 parent f36bd22 commit dd0e97e
Show file tree
Hide file tree
Showing 2 changed files with 20 additions and 21 deletions.
26 changes: 13 additions & 13 deletions lib/find-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,8 @@ PythonFinder.prototype = {
win: win,
pyLauncher: 'py.exe',
winDefaultLocations: [
path.join(process.env.SystemDrive || 'C:', 'Python27', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Python37', 'python.exe')
path.join(process.env.SystemDrive || 'C:', 'Python37', 'python.exe'),
path.join(process.env.SystemDrive || 'C:', 'Python27', 'python.exe')
],

// Logs a message at verbose level, but also saves it to be displayed later
Expand Down Expand Up @@ -88,14 +88,14 @@ PythonFinder.prototype = {
arg: this.env.PYTHON
},
{
before: () => { this.addLog('checking if "python" can be used') },
before: () => { this.addLog('checking if "python3" can be used') },
check: this.checkCommand,
arg: 'python'
arg: 'python3'
},
{
before: () => { this.addLog('checking if "python3" can be used') },
before: () => { this.addLog('checking if "python" can be used') },
check: this.checkCommand,
arg: 'python3'
arg: 'python'
},
{
before: () => { this.addLog('checking if "python2" can be used') },
Expand All @@ -105,13 +105,6 @@ PythonFinder.prototype = {
]

if (this.win) {
checks.push({
before: () => {
this.addLog(
'checking if the py launcher can be used to find Python 2')
},
check: this.checkPyLauncher
})
for (var i = 0; i < this.winDefaultLocations.length; ++i) {
const location = this.winDefaultLocations[i]
checks.push({
Expand All @@ -123,6 +116,13 @@ PythonFinder.prototype = {
arg: location
})
}
checks.push({
before: () => {
this.addLog(
'checking if the py launcher can be used to find Python 2')
},
check: this.checkPyLauncher
})
}

return checks
Expand Down
15 changes: 7 additions & 8 deletions test/test-find-python.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,8 @@ test('find python - no python, use python launcher', function (t) {
}
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
} else if (f.winDefaultLocations.includes(program)) {
cb(new Error('not found'))
} else if (/sys\.version_info/.test(args[args.length - 1])) {
if (program === 'Z:\\snake.exe') {
cb(null, '2.7.14')
Expand All @@ -178,24 +180,21 @@ test('find python - no python, use python launcher', function (t) {
})

test('find python - no python, no python launcher, good guess', function (t) {
t.plan(4)
t.plan(2)

var re = /C:[\\/]Python27[\\/]python[.]exe/
var re = /C:[\\/]Python37[\\/]python[.]exe/
var f = new TestPythonFinder(null, done)
f.win = true

f.execFile = function (program, args, opts, cb) {
if (program === 'py.exe') {
f.execFile = function (program, args, opts, cb) {
poison(f, 'execFile')
t.ok(re.test(program))
t.ok(/sys\.version_info/.test(args[args.length - 1]))
cb(null, '2.7.14')
}
return cb(new Error('not found'))
}
if (/sys\.executable/.test(args[args.length - 1])) {
cb(new Error('not found'))
} else if (re.test(program) &&
/sys\.version_info/.test(args[args.length - 1])) {
cb(null, '3.7.3')
} else {
t.fail()
}
Expand Down

0 comments on commit dd0e97e

Please sign in to comment.