-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfind_python.bat
More file actions
139 lines (114 loc) · 4.4 KB
/
find_python.bat
File metadata and controls
139 lines (114 loc) · 4.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
@echo off
setlocal enabledelayedexpansion
REM https://github.com/RecklessDev/PythonInstallFinder
REM MIT License
REM This scripts looks on the system to find the specified python versions
REM It returns in environment variable PYTHON_INSTALL_DIR the folder where python.exe is found
REM Set the script defaults so they behave consistently
set INTERNAL_SUPPORTED_PYTHON_VERSIONS=
set INTERNAL_PRINT_PYTHON_PATH=0
set PYTHON_INSTALL_DIR=
REM Process command line arguments
:PROCESS_ARGS
if "%1" NEQ "" (
if "%1" == "-pv" (
goto PROCESS_ARGS_PYTHON_VERSIONS
) else if "%1" == "-h" (
goto HELP
) else if "%1" == "-p" (
set INTERNAL_PRINT_PYTHON_PATH=1
) else if "%1" == "-l" (
goto LIST_ALL_PYTHON_VERSIONS_FROM_REG
) else (
echo Skipping unrecognized command argument %1
)
SHIFT
goto PROCESS_ARGS
)
goto END_PROCESS_ARGS
REM Format for -pv (meaning python version) is:
REM -py version_1 version_2 version_3 ... version_n
REM example: -py 3.6 3.7
REM For 32 bits the version are of the form M.m-32
:PROCESS_ARGS_PYTHON_VERSIONS
SHIFT
if "%1" == "" goto END_PROCESS_ARGS
SET ARG_PY_V=%1
if "%ARG_PY_V:~0,1%" == "-" goto PROCESS_ARGS
SET INTERNAL_SUPPORTED_PYTHON_VERSIONS=%INTERNAL_SUPPORTED_PYTHON_VERSIONS% %ARG_PY_V% %ARG_PY_V%-32
goto PROCESS_ARGS_PYTHON_VERSIONS
:HELP
echo This script tries to find a valid python installation on the machine of one of the versions provided.
echo First search is done in PATH. If a python is found and its version is one of the accepted one then it selects it.
echo If PATH search failed then it looks into the registry for an installation matching the versions.
echo If it was not found then it simply returns 1.
echo When the python installation is found it sets the variable PYTHON_INSTALL_DIR and returns error code 0
echo.
echo Possible arguments:
echo -pv v_1 v_2 ... v_n
echo Mandatory; used to specify the versions. To search for python 3.6 and 3.7 for both 32 and 64 bit:
echo -pv 3.6 3.7
echo The first version should be the most preferred one to look for.
echo -h
echo This help.
echo -p
echo Prints the found python path to the console; should be used with -pv option.
goto END
REM At this point all known parameters should had been processed
:END_PROCESS_ARGS
REM Do validation for inputs
if "%INTERNAL_SUPPORTED_PYTHON_VERSIONS%" == "" (
echo No python version provided. Please use argument -pv to provide a list of python to check against
goto HELP
)
REM Look in path first
for %%a in (python.exe) do set PYTHON_INSTALL_DIR=%%~dp$PATH:a
if "%PYTHON_INSTALL_DIR%" NEQ "" (
for /f "tokens=1,2 usebackq" %%b in (`%PYTHON_INSTALL_DIR%/python.exe -V`) do (
for %%a in (%INTERNAL_SUPPORTED_PYTHON_VERSIONS%) do (
set CONSOLE_PY_VERSION=%%c
if "!CONSOLE_PY_VERSION:~0,3!" == "%%a" (
set PYTHONVERSION=%%b
goto END
)
)
)
REM If we get here it means python is in path but not one of the accepted versions
set PYTHON_INSTALL_DIR=
)
REM Search for python installation in the registry; this can be in different places depending on the OS
set PYTHONKEY=HKLM\SOFTWARE\Wow6432Node\Python\PythonCore
reg query %PYTHONKEY% >nul 2>nul
if not errorlevel 1 goto GetPythonVersion
set PYTHONKEY=HKLM\SOFTWARE\Python\PythonCore
reg query %PYTHONKEY% >nul 2>nul
if not errorlevel 1 goto GetPythonVersion
SET PYTHONKEY=HKCU\SOFTWARE\Wow6432Node\Python\PythonCore
reg query %PYTHONKEY% >nul 2>nul
if not errorlevel 1 goto GetPythonVersion
SET PYTHONKEY=HKCU\SOFTWARE\Python\PythonCore
reg query %PYTHONKEY% >nul 2>nul
if not errorlevel 1 goto GetPythonVersion
REM At this point we couldn't find registry keys for any python installation
goto ERROR
:GetPythonVersion
for %%a in (%INTERNAL_SUPPORTED_PYTHON_VERSIONS%) do (
reg query %PYTHONKEY%\%%a\InstallPath >nul 2>nul
if not errorlevel 1 (
set PYTHONVERSION=%%a
GOTO GetPythonPath
)
)
goto ERROR
:GetPythonPath
for /f "tokens=3" %%a in ('reg query %PYTHONKEY%\%PYTHONVERSION%\InstallPath /V "" ^|findstr /ri "REG_SZ"') do set PYTHON_INSTALL_DIR=%%a
goto END
:ERROR
if %INTERNAL_PRINT_PYTHON_PATH%==1 echo Python installation not found.
set PYTHON_INSTALL_DIR=
exit /B 1
:END
if %PYTHON_INSTALL_DIR% == "" goto ERROR
if %INTERNAL_PRINT_PYTHON_PATH%==1 echo %PYTHON_INSTALL_DIR%
endlocal & set PYTHON_INSTALL_DIR=%PYTHON_INSTALL_DIR%
exit /B 0