• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1@IF NOT DEFINED DEBUG_HELPER @ECHO OFF
2
3echo Looking for Python
4setlocal enabledelayedexpansion
5
6:: Use python.exe if in %PATH%
7set need_path=0
8for /f "delims=" %%a in ('where python.exe 2^> nul') do (
9  set p=%%~dpa
10  goto :found-python
11)
12
13:: Query the 3 locations mentioned in PEP 514 for a Python InstallPath
14set need_path=1
15for %%k in ( "HKCU\Software", "HKLM\SOFTWARE", "HKLM\Software\Wow6432Node") do (
16  call :find-versions %%k
17  if not errorlevel 1 goto :found-python
18)
19
20goto :no-python
21
22
23:: Find Python installations in a registry location
24:find-versions
25for /f "delims=" %%a in ('reg query "%~1\Python\PythonCore" /f * /k 2^> nul ^| findstr /r ^^HK') do (
26  call :read-installpath %%a
27  if not errorlevel 1 exit /b 0
28)
29exit /b 1
30
31:: Read the InstallPath of a given Environment Key to %p%
32:: https://www.python.org/dev/peps/pep-0514/#installpath
33:read-installpath
34:: %%a will receive everything before ), might have spaces depending on language
35:: %%b will receive *, corresponding to everything after )
36:: %%c will receive REG_SZ
37:: %%d will receive the path, including spaces
38for /f "skip=2 tokens=1* delims=)" %%a in ('reg query "%1\InstallPath" /ve /t REG_SZ 2^> nul') do (
39  for /f "tokens=1*" %%c in ("%%b") do (
40    if not "%%c"=="REG_SZ" exit /b 1
41    set "p=%%d"
42    exit /b 0
43  )
44)
45exit /b 1
46
47:found-python
48echo Python found in %p%\python.exe
49endlocal ^
50  & set "pt=%p%" ^
51  & set "need_path_ext=%need_path%"
52set "VCBUILD_PYTHON_LOCATION=%pt%\python.exe"
53if %need_path_ext%==1 set "PATH=%pt%;%PATH%"
54set "pt="
55set "need_path_ext="
56exit /b 0
57
58:no-python
59echo Could not find Python.
60exit /b 1
61