Last active
February 28, 2025 07:47
-
-
Save achadha235/ec1d231f79be1aefdffa97e94c3f867e to your computer and use it in GitHub Desktop.
Debug python installs on your machine.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env bun | |
| // This script searches through the system PATH environment variable to locate Python installations | |
| // It checks each directory in PATH for both 'python' and 'python3' executables | |
| // When found, it prints the directory location and specific executable paths | |
| // This can help diagnose which Python versions are available and their resolution order | |
| // this script is written for bun - to run it use `bun run script.ts` | |
| // it should also be runnable with node using `npx tsx ./script.ts` | |
| import { existsSync } from "fs"; | |
| import { join } from "path"; | |
| import { spawnSync } from "child_process"; | |
| // Function to get Python version | |
| function getPythonVersion(pythonPath: string): string { | |
| try { | |
| const result = spawnSync(pythonPath, ["--version"]); | |
| if (result.status === 0) { | |
| // Trim to remove any trailing newlines | |
| return result.stdout.toString().trim() || result.stderr.toString().trim(); | |
| } else { | |
| return "Unable to determine version"; | |
| } | |
| } catch (error) { | |
| return "Error getting version"; | |
| } | |
| } | |
| // Get the PATH environment variable and split it into individual directories | |
| const pathEnv = process.env.PATH || ""; | |
| const pathDirs = pathEnv.split(":"); | |
| console.log("Checking for Python installations in your PATH...\n"); | |
| // Track if we found any Python installations | |
| let foundPython = false; | |
| // Check each directory in PATH | |
| for (const dir of pathDirs) { | |
| // Check for python and python3 executables | |
| const pythonPath = join(dir, "python"); | |
| const python3Path = join(dir, "python3"); | |
| const hasPython = existsSync(pythonPath); | |
| const hasPython3 = existsSync(python3Path); | |
| // If either python or python3 exists in this directory, print it | |
| if (hasPython || hasPython3) { | |
| foundPython = true; | |
| console.log(`Found Python in: ${dir}`); | |
| if (hasPython) { | |
| const version = getPythonVersion(pythonPath) | |
| .replace("Python ", "") | |
| .trim(); | |
| console.log(` - python@${version} executable: ${pythonPath}`); | |
| } | |
| if (hasPython3) { | |
| const version = getPythonVersion(python3Path) | |
| .replace("Python ", "") | |
| .trim(); | |
| console.log(` - python3@${version} executable: ${python3Path}`); | |
| } | |
| console.log(""); // Empty line for better readability | |
| } | |
| } | |
| if (!foundPython) { | |
| console.log("No Python installations found in your PATH."); | |
| } | |
| console.log( | |
| "Note: Python installations are listed in the order they would be resolved by your system." | |
| ); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment