Repository navigation
Expand file tree
/
Copy pathdetect_python.cmd
More file actions
166 lines (153 loc) · 5.92 KB
/
Copy pathdetect_python.cmd
File metadata and controls
166 lines (153 loc) · 5.92 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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
@echo off
setlocal enabledelayedexpansion
REM ============================================
REM Shared Python interpreter detection routine
REM Used by: run.bat, build_universal.bat
REM
REM Each caller must set LOG_PREFIX before calling.
REM run.bat: set "LOG_PREFIX=echo [Python]"
REM build_universal.bat: set "LOG_PREFIX=call :log_echo [Python]"
REM
REM Sets: PYTHON_EXE, PIP_EXE
REM ============================================
set "PYTHON_EXE="
set "PIP_EXE="
REM --- 1. Project virtualenv (must be runnable - base interpreter not removed) ---
if exist "%VENV_DIR%\Scripts\python.exe" (
"%VENV_DIR%\Scripts\python.exe" --version >nul 2>&1
if !errorlevel! equ 0 (
set "PYTHON_EXE=%VENV_DIR%\Scripts\python.exe"
if exist "%VENV_DIR%\Scripts\pip.exe" (
set "PIP_EXE=%VENV_DIR%\Scripts\pip.exe"
) else (
set "PIP_EXE=%VENV_DIR%\Scripts\python.exe -m pip"
)
%LOG_PREFIX% Project virtualenv detected: %VENV_DIR%
goto :exit_detect
) else (
%LOG_PREFIX% .venv exists but interpreter is broken ^(base Python missing^), skipping
)
)
REM --- 2. PYTHON_HOME ---
if defined PYTHON_HOME (
if exist "!PYTHON_HOME!\python.exe" (
set "PYTHON_EXE=!PYTHON_HOME!\python.exe"
if exist "!PYTHON_HOME!\Scripts\pip.exe" (
set "PIP_EXE=!PYTHON_HOME!\Scripts\pip.exe"
) else (
set "PIP_EXE=!PYTHON_HOME!\python.exe -m pip"
)
%LOG_PREFIX% PYTHON_HOME detected: !PYTHON_HOME!
goto :exit_detect
)
)
REM --- 3. pyenv-win ---
set "PYENV_ROOT=%USERPROFILE%\.pyenv\pyenv-win"
REM 3a. Try pyenv-win shims first (routes to active version)
if exist "!PYENV_ROOT!\shims\python.bat" (
for /f "usebackq tokens=1 delims= " %%s in ("!PYENV_ROOT!\shims\python.bat") do (
set "SHIM_LINE=%%s"
if not "!SHIM_LINE!"=="@echo" (
set "SHIM_LINE=!SHIM_LINE:"=!"
if /i "!SHIM_LINE:~-10!"=="python.exe" (
if exist "!SHIM_LINE!" (
set "PYTHON_EXE=!SHIM_LINE!"
for %%p in ("!SHIM_LINE!") do set "PIP_EXE=%%~dppScripts\pip.exe"
if not exist "!PIP_EXE!" set "PIP_EXE=!SHIM_LINE! -m pip"
%LOG_PREFIX% pyenv-win shims detected: !PYTHON_EXE!
goto :exit_detect
)
)
)
)
)
REM 3b. Try the version file (records active pyenv-win version)
if exist "!PYENV_ROOT!\versions" (
set "PYENV_ACTIVE="
if exist "!PYENV_ROOT!\version" (
set /p PYENV_ACTIVE=<"!PYENV_ROOT!\version"
)
if not "!PYENV_ACTIVE!"=="" (
if exist "!PYENV_ROOT!\versions\!PYENV_ACTIVE!\python.exe" (
set "PYTHON_EXE=!PYENV_ROOT!\versions\!PYENV_ACTIVE!\python.exe"
set "PIP_EXE=!PYENV_ROOT!\versions\!PYENV_ACTIVE!\Scripts\pip.exe"
%LOG_PREFIX% pyenv-win active version: !PYENV_ACTIVE!
%LOG_PREFIX% Path: !PYTHON_EXE!
goto :exit_detect
)
)
REM Fallback: pick the last version directory listed (alphabetical order)
set "PYENV_PICK="
for /f "delims=" %%d in ('dir /b /ad /on "!PYENV_ROOT!\versions" 2^>nul') do (
if exist "!PYENV_ROOT!\versions\%%d\python.exe" set "PYENV_PICK=%%d"
)
if not "!PYENV_PICK!"=="" (
set "PYTHON_EXE=!PYENV_ROOT!\versions\!PYENV_PICK!\python.exe"
set "PIP_EXE=!PYENV_ROOT!\versions\!PYENV_PICK!\Scripts\pip.exe"
%LOG_PREFIX% pyenv-win version detected: !PYENV_PICK!
%LOG_PREFIX% Path: !PYTHON_EXE!
goto :exit_detect
)
)
REM --- 4. Per-user python.org install (LOCALAPPDATA) ---
set "PY_PICK_DIR="
for /f "delims=" %%d in ('dir /b /ad /on "%LOCALAPPDATA%\Programs\Python" 2^>nul') do (
if exist "%LOCALAPPDATA%\Programs\Python\%%d\python.exe" set "PY_PICK_DIR=%LOCALAPPDATA%\Programs\Python\%%d"
)
if not "!PY_PICK_DIR!"=="" (
set "PYTHON_EXE=!PY_PICK_DIR!\python.exe"
set "PIP_EXE=!PY_PICK_DIR!\Scripts\pip.exe"
%LOG_PREFIX% Per-user install detected: !PY_PICK_DIR!
goto :exit_detect
)
REM --- 5a. System-wide python.org install (Program Files) ---
set "PY_PICK_DIR="
for /f "delims=" %%d in ('dir /b /ad /on "%ProgramFiles%\Python*" 2^>nul') do (
if exist "%ProgramFiles%\%%d\python.exe" set "PY_PICK_DIR=%ProgramFiles%\%%d"
)
if not "!PY_PICK_DIR!"=="" (
set "PYTHON_EXE=!PY_PICK_DIR!\python.exe"
set "PIP_EXE=!PY_PICK_DIR!\Scripts\pip.exe"
%LOG_PREFIX% System install detected: !PY_PICK_DIR!
goto :exit_detect
)
REM --- 5b. System-wide python.org install (Program Files x86) ---
set "PY_PICK_DIR="
for /f "delims=" %%d in ('dir /b /ad /on "%ProgramFiles(x86)%\Python*" 2^>nul') do (
if exist "%ProgramFiles(x86)%\%%d\python.exe" set "PY_PICK_DIR=%ProgramFiles(x86)%\%%d"
)
if not "!PY_PICK_DIR!"=="" (
set "PYTHON_EXE=!PY_PICK_DIR!\python.exe"
set "PIP_EXE=!PY_PICK_DIR!\Scripts\pip.exe"
%LOG_PREFIX% System install (x86) detected: !PY_PICK_DIR!
goto :exit_detect
)
REM --- 6. Legacy C:\Python* ---
set "PY_PICK_DIR="
for /f "delims=" %%d in ('dir /b /ad /on "C:\Python*" 2^>nul') do (
if exist "C:\%%d\python.exe" set "PY_PICK_DIR=C:\%%d"
)
if not "!PY_PICK_DIR!"=="" (
set "PYTHON_EXE=!PY_PICK_DIR!\python.exe"
set "PIP_EXE=!PY_PICK_DIR!\Scripts\pip.exe"
%LOG_PREFIX% Legacy install detected: !PY_PICK_DIR!
goto :exit_detect
)
REM --- 7. System PATH ---
where python >nul 2>&1
if !errorlevel! equ 0 (
for /f "delims=" %%p in ('where python 2^>nul') do (
if not defined PYTHON_EXE set "PYTHON_EXE=%%p"
)
set "PIP_EXE=pip"
%LOG_PREFIX% System PATH python detected: !PYTHON_EXE!
goto :exit_detect
)
REM --- Fallback (no Python found) ---
%LOG_PREFIX% No Python interpreter located, falling back to "python" on PATH
set "PYTHON_EXE=python"
set "PIP_EXE=pip"
:exit_detect
REM 将变量提升到调用者作用域(endlocal 销毁本地变量前保存到外层)
endlocal & set "PYTHON_EXE=%PYTHON_EXE%" & set "PIP_EXE=%PIP_EXE%"
goto :eof