• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1@IF NOT DEFINED DEBUG_HELPER @ECHO OFF
2
3echo Looking for Python
4setlocal enabledelayedexpansion
5
6:: To remove the preference for Python 2, but still support it, just remove
7:: the 5 blocks marked with "Python 2:" and the support warnings
8
9:: Python 2: If python.exe is in %Path%, use if it's Python 2
10FOR /F "delims=" %%a IN ('where python.exe 2^> NUL') DO (
11  SET need_path=0
12  SET p=%%~dpa
13  CALL :validate-v2
14  IF NOT ERRORLEVEL 1 GOTO :found-python2
15  GOTO :done-path-v2
16)
17:done-path-v2
18
19:: Python 2: Query the 3 locations mentioned in PEP 514 for a python2 InstallPath
20FOR %%K IN ( "HKCU\Software", "HKLM\SOFTWARE", "HKLM\Software\Wow6432Node") DO (
21  SET need_path=1
22  CALL :find-versions-v2 %%K
23  IF NOT ERRORLEVEL 1 CALL :validate-v2
24  IF NOT ERRORLEVEL 1 GOTO :found-python2
25)
26
27:: Use python.exe if in %PATH%
28set need_path=0
29for /f "delims=" %%a in ('where python.exe 2^> nul') do (
30  set p=%%~dpa
31  goto :found-python
32)
33
34:: Query the 3 locations mentioned in PEP 514 for a Python InstallPath
35set need_path=1
36for %%k in ( "HKCU\Software", "HKLM\SOFTWARE", "HKLM\Software\Wow6432Node") do (
37  call :find-versions %%k
38  if not errorlevel 1 goto :found-python
39)
40
41goto :no-python
42
43
44:: Python 2: Find Python 2 installations in a registry location
45:find-versions-v2
46for /f "delims=" %%a in ('reg query "%~1\Python\PythonCore" /f * /k 2^> nul ^| findstr /r ^^HK ^| findstr "\\2\."') do (
47  call :read-installpath %%a
48  if not errorlevel 1 exit /b 0
49)
50exit /b 1
51
52:: Find Python installations in a registry location
53:find-versions
54for /f "delims=" %%a in ('reg query "%~1\Python\PythonCore" /f * /k 2^> nul ^| findstr /r ^^HK') do (
55  call :read-installpath %%a
56  if not errorlevel 1 exit /b 0
57)
58exit /b 1
59
60:: Read the InstallPath of a given Environment Key to %p%
61:: https://www.python.org/dev/peps/pep-0514/#installpath
62:read-installpath
63:: %%a will receive everything before ), might have spaces depending on language
64:: %%b will receive *, corresponding to everything after )
65:: %%c will receive REG_SZ
66:: %%d will receive the path, including spaces
67for /f "skip=2 tokens=1* delims=)" %%a in ('reg query "%1\InstallPath" /ve /t REG_SZ 2^> nul') do (
68  for /f "tokens=1*" %%c in ("%%b") do (
69    if not "%%c"=="REG_SZ" exit /b 1
70    set "p=%%d"
71    exit /b 0
72  )
73)
74exit /b 1
75
76
77:: Python 2: Check if %p% holds a path to a real python2 executable
78:validate-v2
79IF NOT EXIST "%p%\python.exe" EXIT /B 1
80:: Check if %p% is python2
81"%p%\python.exe" -V 2>&1 | findstr /R "^Python.2.*" > NUL
82EXIT /B %ERRORLEVEL%
83
84
85:: Python 2:
86:found-python2
87echo Python 2 found in %p%\python.exe
88set pyver=2
89goto :done
90
91:found-python
92echo Python found in %p%\python.exe
93echo WARNING: Python 3 is not yet fully supported, to avoid issues Python 2 should be installed.
94set pyver=3
95goto :done
96
97:done
98endlocal ^
99  & set "pt=%p%" ^
100  & set "need_path_ext=%need_path%" ^
101  & set "VCBUILD_PYTHON_VERSION=%pyver%"
102set "VCBUILD_PYTHON_LOCATION=%pt%\python.exe"
103if %need_path_ext%==1 set "PATH=%pt%;%PATH%"
104set "pt="
105set "need_path_ext="
106exit /b 0
107
108:no-python
109echo Could not find Python.
110exit /b 1
111