Skip to content

Commit 753979d

Browse files
committed
Detect the python version based on the py launcher
1 parent a506a1c commit 753979d

1 file changed

Lines changed: 19 additions & 0 deletions

File tree

pre_commit/languages/python.py

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
from pre_commit.envcontext import Var
1010
from pre_commit.languages import helpers
1111
from pre_commit.parse_shebang import find_executable
12+
from pre_commit.util import CalledProcessError
1213
from pre_commit.util import clean_path_on_failure
1314
from pre_commit.util import cmd_output
1415
from pre_commit.xargs import xargs
@@ -40,6 +41,17 @@ def in_env(repo_cmd_runner, language_version):
4041
yield
4142

4243

44+
def _find_by_py_launcher(version): # pragma: no cover (windows only)
45+
if version.startswith('python'):
46+
try:
47+
return cmd_output(
48+
'py', '-{}'.format(version[len('python'):]),
49+
'-c', 'import sys; print(sys.executable)',
50+
)[1].strip()
51+
except CalledProcessError:
52+
pass
53+
54+
4355
def _get_default_version(): # pragma: no cover (platform dependent)
4456
def _norm(path):
4557
_, exe = os.path.split(path.lower())
@@ -66,6 +78,9 @@ def _norm(path):
6678
if find_executable(exe):
6779
return exe
6880

81+
if _find_by_py_launcher(exe):
82+
return exe
83+
6984
# Give a best-effort try for windows
7085
if os.path.exists(r'C:\{}\python.exe'.format(exe.replace('.', ''))):
7186
return exe
@@ -99,6 +114,10 @@ def norm_version(version):
99114
if version_exec and version_exec != version:
100115
return version_exec
101116

117+
version_exec = _find_by_py_launcher(version)
118+
if version_exec:
119+
return version_exec
120+
102121
# If it is in the form pythonx.x search in the default
103122
# place on windows
104123
if version.startswith('python'):

0 commit comments

Comments
 (0)