Skip to content

Commit e1862ca

Browse files
authored
Rollup merge of #98474 - dtolnay:python3, r=Mark-Simulacrum
x.py: Support systems with only `python3` not `python` Fixes #71818 without the pitfalls so far described in previous attempts.
2 parents 7c39776 + 9169905 commit e1862ca

File tree

1 file changed

+25
-4
lines changed

1 file changed

+25
-4
lines changed

x.py

+25-4
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,38 @@
1-
#!/usr/bin/env python
1+
#!/usr/bin/env bash
22

3+
# Modern Linux and macOS systems commonly only have a thing called `python3` and
4+
# not `python`, while Windows commonly does not have `python3`, so we cannot
5+
# directly use python in the shebang and have it consistently work. Instead we
6+
# embed some bash to look for a python to run the rest of the script.
7+
#
8+
# On Windows, `py -3` sometimes works. We need to try it first because `python3`
9+
# sometimes tries to launch the app store on Windows.
10+
'''':
11+
for PYTHON in "py -3" python3 python python2; do
12+
if command -v $PYTHON >/dev/null; then
13+
exec $PYTHON "$0" "$@"
14+
break
15+
fi
16+
done
17+
echo "$0: error: did not find python installed" >&2
18+
exit 1
19+
'''
20+
21+
# The rest of this file is Python.
22+
#
323
# This file is only a "symlink" to bootstrap.py, all logic should go there.
424
525
import os
626
import sys
727
828
# If this is python2, check if python3 is available and re-execute with that
929
# interpreter.
30+
#
31+
# `./x.py` would not normally benefit from this because the bash above tries
32+
# python3 before 2, but this matters if someone ran `python x.py` and their
33+
# system's `python` is python2.
1034
if sys.version_info.major < 3:
1135
try:
12-
# On Windows, `py -3` sometimes works.
13-
# Try this first, because 'python3' sometimes tries to launch the app
14-
# store on Windows
1536
os.execvp("py", ["py", "-3"] + sys.argv)
1637
except OSError:
1738
try:

0 commit comments

Comments
 (0)