Skip to content

Commit a6ac6b2

Browse files
author
Yuki Okushi
authored
Rollup merge of rust-lang#103007 - albertlarsan68:better-python-discovery, r=jyn514
Add better python discovery The Microsoft Store version of Python installs itself as `pythonM.m`, with `M` being the major version and `m` the minor. The `x.ps1` script will now search for python executables whose command matches the regex `python\d`. The `\d` at the end is to protect from using the `pythonw` versions, which do not work as standard python.
2 parents 024207a + c83ddae commit a6ac6b2

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

Diff for: x

+6
Original file line numberDiff line numberDiff line change
@@ -29,5 +29,11 @@ for SEARCH_PYTHON in py python3 python python2; do
2929
exec "$python" $extra_arg "$xpy" "$@"
3030
fi
3131
done
32+
33+
python=$(bash -c "compgen -c python" | grep '^python[2-3]\.[0-9]\+$' | head -n1)
34+
if ! [ "$python" = "" ]; then
35+
exec "$python" "$xpy" "$@"
36+
fi
37+
3238
echo "$0: error: did not find python installed" >&2
3339
exit 1

Diff for: x.ps1

+12-1
Original file line numberDiff line numberDiff line change
@@ -10,11 +10,15 @@ foreach ($arg in $args) {
1010
$xpy_args += """$arg"""
1111
}
1212

13+
function Get-Application($app) {
14+
return Get-Command $app -ErrorAction SilentlyContinue -CommandType Application
15+
}
16+
1317
foreach ($python in "py", "python3", "python", "python2") {
1418
# NOTE: this only tests that the command exists in PATH, not that it's actually
1519
# executable. The latter is not possible in a portable way, see
1620
# https://github.com/PowerShell/PowerShell/issues/12625.
17-
if (Get-Command $python -ErrorAction SilentlyContinue) {
21+
if (Get-Application $python) {
1822
if ($python -eq "py") {
1923
# Use python3, not python2
2024
$xpy_args = @("-3") + $xpy_args
@@ -24,5 +28,12 @@ foreach ($python in "py", "python3", "python", "python2") {
2428
}
2529
}
2630

31+
$found = (Get-Application "python*" | Where-Object {$_.name -match '^python[2-3]\.[0-9]+(\.exe)?$'})
32+
if (($null -ne $found) -and ($found.Length -ge 1)) {
33+
$python = $found[0]
34+
$process = Start-Process -NoNewWindow -Wait -PassThru $python $xpy_args
35+
Exit $process.ExitCode
36+
}
37+
2738
Write-Error "${PSCommandPath}: error: did not find python installed"
2839
Exit 1

0 commit comments

Comments
 (0)