diff --git a/x.py b/x.py
index 7973730ef177c..60cc589cd6388 100755
--- a/x.py
+++ b/x.py
@@ -1,4 +1,18 @@
-#!/usr/bin/env python
+# The beginning of this script is both valid shell and valid python, such that
+# the script starts with the shell and is reexecuted with the right python.
+# This works because shells only execute a line at a time.
+# Thanks to `./mach` from servo for the idea!
+''':' && {
+exists() { command -v "$1" >/dev/null 2>&1; }
+if exists python3; then
+    exec python3 "$0" "$@"
+elif exists python; then
+    exec python "$0" "$@"
+else
+    exec python2 "$0" "$@"
+fi
+}
+'''
 
 # This file is only a "symlink" to bootstrap.py, all logic should go there.