MCPcopy Create free account
hub / github.com/codefl0w/mtkclient-windows-installer / find_python_executable

Function find_python_executable

mtkclient_installer.py:133–170  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

131
132
133def find_python_executable() -> str:
134 is_frozen = getattr(sys, 'frozen', False)
135
136 py = shutil.which("python")
137 if py:
138 if not (is_frozen and os.path.samefile(py, sys.executable)):
139 return py
140
141 roots = [
142 Path(os.environ.get("ProgramFiles", r"C:\Program Files")) / "Python",
143 Path(os.environ.get("LOCALAPPDATA", "")) / "Programs" / "Python",
144 Path(r"C:\Python")
145 ]
146
147 found_exes = []
148 for root in roots:
149 if root.exists():
150 for folder in root.iterdir():
151 if folder.is_dir() and folder.name.lower().startswith("python3"):
152 exe_path = folder / "python.exe"
153 if exe_path.exists():
154 found_exes.append(exe_path)
155
156 if found_exes:
157 found_exes.sort(key=lambda x: x.parent.name, reverse=True)
158 return str(found_exes[0])
159
160 pylauncher = shutil.which("py")
161 if pylauncher:
162 try:
163 out = subprocess.check_output([pylauncher, "-3", "-c", "import sys;print(sys.executable)"],
164 text=True, timeout=5).strip()
165 if out and Path(out).exists():
166 return out
167 except:
168 pass
169
170 return sys.executable if not is_frozen else ""
171
172
173class CommandResult:

Callers 4

step_verifyMethod · 0.85

Calls

no outgoing calls

Tested by

no test coverage detected