()
| 131 | |
| 132 | |
| 133 | def 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 | |
| 173 | class CommandResult: |
no outgoing calls
no test coverage detected