import { execSync } from "child_process"; import { writeFileSync } from "fs"; import path from "path"; const candidates = [ "/usr/bin/python3.9", "python3", "python" ]; const pythonPathFile = path.resolve(process.cwd(), "python-path.txt"); function checkPython(pythonCmd) { try { execSync(`${pythonCmd} --version`, { stdio: "ignore" }); execSync(`${pythonCmd} -m pip --version`, { stdio: "ignore" }); return true; } catch { return false; } } function installDeps(pythonCmd) { try { console.log(`Using Python command: ${pythonCmd}`); execSync(`${pythonCmd} -m pip install -r requirements.txt`, { stdio: "inherit" }); writeFileSync(pythonPathFile, pythonCmd, "utf-8"); console.log(`✅ Python packages installed successfully. Saved Python path to ${pythonPathFile}`); process.exit(0); } catch (err) { console.error("Failed to install Python packages with", pythonCmd); } } function main() { for (const cmd of candidates) { if (checkPython(cmd)) { installDeps(cmd); return; } } console.error( "\n⚠️ Python or pip not found or pip is broken.\n" + "Please install Python 3 and pip manually, then run:\n\n" + " pip install -r requirements.txt\n" ); process.exit(1); } main();