Skip to content

Commit c83ddae

Browse files
Add better python discovery
`x.ps1` and `x` will now search for python executables like `python3.9` and `python3.10.exe`
1 parent 4596f4f commit c83ddae

File tree

2 files changed

+18
-1
lines changed

2 files changed

+18
-1
lines changed

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

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)