Skip to content

Commit dd0e97e

Browse files
sam-githubrvagg
authored andcommitted
lib: try to find python after python3
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 <[email protected]> Reviewed-By: Rod Vagg <[email protected]> Reviewed-By: João Reis <[email protected]>
1 parent f36bd22 commit dd0e97e

File tree

2 files changed

+20
-21
lines changed

2 files changed

+20
-21
lines changed

lib/find-python.js

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ PythonFinder.prototype = {
2626
win: win,
2727
pyLauncher: 'py.exe',
2828
winDefaultLocations: [
29-
path.join(process.env.SystemDrive || 'C:', 'Python27', 'python.exe'),
30-
path.join(process.env.SystemDrive || 'C:', 'Python37', 'python.exe')
29+
path.join(process.env.SystemDrive || 'C:', 'Python37', 'python.exe'),
30+
path.join(process.env.SystemDrive || 'C:', 'Python27', 'python.exe')
3131
],
3232

3333
// Logs a message at verbose level, but also saves it to be displayed later
@@ -88,14 +88,14 @@ PythonFinder.prototype = {
8888
arg: this.env.PYTHON
8989
},
9090
{
91-
before: () => { this.addLog('checking if "python" can be used') },
91+
before: () => { this.addLog('checking if "python3" can be used') },
9292
check: this.checkCommand,
93-
arg: 'python'
93+
arg: 'python3'
9494
},
9595
{
96-
before: () => { this.addLog('checking if "python3" can be used') },
96+
before: () => { this.addLog('checking if "python" can be used') },
9797
check: this.checkCommand,
98-
arg: 'python3'
98+
arg: 'python'
9999
},
100100
{
101101
before: () => { this.addLog('checking if "python2" can be used') },
@@ -105,13 +105,6 @@ PythonFinder.prototype = {
105105
]
106106

107107
if (this.win) {
108-
checks.push({
109-
before: () => {
110-
this.addLog(
111-
'checking if the py launcher can be used to find Python 2')
112-
},
113-
check: this.checkPyLauncher
114-
})
115108
for (var i = 0; i < this.winDefaultLocations.length; ++i) {
116109
const location = this.winDefaultLocations[i]
117110
checks.push({
@@ -123,6 +116,13 @@ PythonFinder.prototype = {
123116
arg: location
124117
})
125118
}
119+
checks.push({
120+
before: () => {
121+
this.addLog(
122+
'checking if the py launcher can be used to find Python 2')
123+
},
124+
check: this.checkPyLauncher
125+
})
126126
}
127127

128128
return checks

test/test-find-python.js

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,8 @@ test('find python - no python, use python launcher', function (t) {
159159
}
160160
if (/sys\.executable/.test(args[args.length - 1])) {
161161
cb(new Error('not found'))
162+
} else if (f.winDefaultLocations.includes(program)) {
163+
cb(new Error('not found'))
162164
} else if (/sys\.version_info/.test(args[args.length - 1])) {
163165
if (program === 'Z:\\snake.exe') {
164166
cb(null, '2.7.14')
@@ -178,24 +180,21 @@ test('find python - no python, use python launcher', function (t) {
178180
})
179181

180182
test('find python - no python, no python launcher, good guess', function (t) {
181-
t.plan(4)
183+
t.plan(2)
182184

183-
var re = /C:[\\/]Python27[\\/]python[.]exe/
185+
var re = /C:[\\/]Python37[\\/]python[.]exe/
184186
var f = new TestPythonFinder(null, done)
185187
f.win = true
186188

187189
f.execFile = function (program, args, opts, cb) {
188190
if (program === 'py.exe') {
189-
f.execFile = function (program, args, opts, cb) {
190-
poison(f, 'execFile')
191-
t.ok(re.test(program))
192-
t.ok(/sys\.version_info/.test(args[args.length - 1]))
193-
cb(null, '2.7.14')
194-
}
195191
return cb(new Error('not found'))
196192
}
197193
if (/sys\.executable/.test(args[args.length - 1])) {
198194
cb(new Error('not found'))
195+
} else if (re.test(program) &&
196+
/sys\.version_info/.test(args[args.length - 1])) {
197+
cb(null, '3.7.3')
199198
} else {
200199
t.fail()
201200
}

0 commit comments

Comments
 (0)