-
Notifications
You must be signed in to change notification settings - Fork 5
/
findpython.bat
121 lines (93 loc) · 3.78 KB
/
findpython.bat
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
@echo off
rem ----------------------------------------
rem findpython.bat by SpiderDave
rem ----------------------------------------
rem usage:
rem use from another .bat file like this:
rem
rem call findpython.bat 1
rem if %errorlevel% NEQ 0 goto error
rem goto success
rem
rem :error
rem echo "Error!"
rem goto theend
rem
rem :success
rem %pycmd% myScript.py
rem
rem :theend
set windowsversion=
for /f "tokens=4-5 delims=. " %%i in ('ver') do set VERSION=%%i.%%j
if "%version%" == "6.3" set windowsversion=8
if "%version%" == "6.2" set windowsversion=8
if "%version%" == "6.1" set windowsversion=7
if "%version%" == "6.0" set windowsversion=Vista
if "%version%" == "10.0" set windowsversion=10
echo Detecting OS: Windows %windowsversion%
set dopause=0
rem turn on pause at the end if we're running this by itself
if x%1 equ x set dopause=1
rem -- find python --------------------
echo Locating python...
echo Attempt 1: "py"
py --version 2>NUL
if %errorlevel% EQU 0 set pycmd=py&goto foundpython
echo Attempt 2: "python"
python -c "from sys import version_info as v;_=0/int(v.major/3)">nul 2>&1
if %errorlevel% EQU 0 set pycmd=python&goto foundpython
echo Attempt 3: "python" using "python" environment variable
rem check for environment variable
if %python%X EQU X goto attempt4
rem test "%python%/python"
%python%\python -c "from sys import version_info as v;_=0/int(v.major/3)">nul 2>&1
if %errorlevel% EQU 0 set pycmd=%python%\python&goto foundpython
:attempt4
echo Attempt 4: Registry
rem Minimum target is 3.6 so we can use f-strings.
set pyval=ExecutablePath
rem todo:
rem Find out if this worlks on Windows 10
rem check current user keys
set pysearchkey=HKEY_CURRENT_USER\Software\Python\PythonCore\3.9\InstallPath
for /f "tokens=2,*" %%a in ('reg query %pysearchkey% /v %pyval% ^| findstr %pyval%') do (set pycmd=%%b)
if %pycmd%x NEQ x goto foundpython
set pysearchkey=HKEY_CURRENT_USER\Software\Python\PythonCore\3.8\InstallPath
for /f "tokens=2,*" %%a in ('reg query %pysearchkey% /v %pyval% ^| findstr %pyval%') do (set pycmd=%%b)
if %pycmd%x NEQ x goto foundpython
set pysearchkey=HKEY_CURRENT_USER\Software\Python\PythonCore\3.7\InstallPath
for /f "tokens=2,*" %%a in ('reg query %pysearchkey% /v %pyval% ^| findstr %pyval%') do (set pycmd=%%b)
if %pycmd%x NEQ x goto foundpython
set pysearchkey=HKEY_CURRENT_USER\Software\Python\PythonCore\3.6\InstallPath
for /f "tokens=2,*" %%a in ('reg query %pysearchkey% /v %pyval% ^| findstr %pyval%') do (set pycmd=%%b)
if %pycmd%x NEQ x goto foundpython
rem check local machine keys
set pysearchkey=HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.9\InstallPath
for /f "tokens=2,*" %%a in ('reg query %pysearchkey% /v %pyval% ^| findstr %pyval%') do (set pycmd=%%b)
if %pycmd%x NEQ x goto foundpython
set pysearchkey=HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.8\InstallPath
for /f "tokens=2,*" %%a in ('reg query %pysearchkey% /v %pyval% ^| findstr %pyval%') do (set pycmd=%%b)
if %pycmd%x NEQ x goto foundpython
set pysearchkey=HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.7\InstallPath
for /f "tokens=2,*" %%a in ('reg query %pysearchkey% /v %pyval% ^| findstr %pyval%') do (set pycmd=%%b)
if %pycmd%x NEQ x goto foundpython
set pysearchkey=HKEY_LOCAL_MACHINE\SOFTWARE\Python\PythonCore\3.6\InstallPath
for /f "tokens=2,*" %%a in ('reg query %pysearchkey% /v %pyval% ^| findstr %pyval%') do (set pycmd=%%b)
if %pycmd%x NEQ x goto foundpython
:pythonnotfound
set errormessage=Could not find python
goto error
:foundpython
echo.
echo Found.
rem -----------------------------------
goto success
:error
echo.
echo.ERROR: %errormessage%
echo.
if %dopause% NEQ 0 pause
goto theend
:success
if %dopause% NEQ 0 pause
:theend