fix: install pip packages using python executable

This commit is contained in:
Amin Yahyaabadi 2022-02-05 11:59:21 -08:00
parent 94827f017e
commit b7f10afb07
3 changed files with 22 additions and 15 deletions

2
dist/setup_cpp.js vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -8,27 +8,34 @@ import { setupPython } from "../../python/python"
import { isBinUptoDate } from "./version" import { isBinUptoDate } from "./version"
import { join } from "path" import { join } from "path"
import { getVersion } from "../../default_versions" import { getVersion } from "../../default_versions"
import { InstallationInfo } from "./setupBin"
let pip: string | undefined let python: string | undefined
let binDir: string | undefined let binDir: string | undefined
let tried = false
/** A function that installs a package using pip */ /** A function that installs a package using pip */
export async function setupPipPack(name: string, version?: string) { export async function setupPipPack(name: string, version?: string): Promise<InstallationInfo> {
// setup python and pip if needed // setup python and pip if needed
if (pip === undefined) { if (python === undefined) {
if (which.sync("pip3", { nothrow: true }) !== null) { if (which.sync("python3", { nothrow: true }) !== null) {
pip = "pip3" python = "python3"
} else if (which.sync("pip", { nothrow: true }) !== null && (await isBinUptoDate("python", "3.0.0"))) { } else if (which.sync("python", { nothrow: true }) !== null && (await isBinUptoDate("python", "3.0.0"))) {
pip = "pip" python = "python"
} else { } else {
info("pip3 was not found. Installing python") info("python3 was not found. Installing python")
await setupPython(getVersion("python", undefined), "", process.arch) await setupPython(getVersion("python", undefined), "", process.arch)
pip = "pip3" // try again
if (tried) {
throw new Error("Failed to install python")
}
tried = true
return setupPipPack(name, version)
} }
} }
execa.sync(pip, ["install", version !== undefined && version !== "" ? `${name}==${version}` : name], { execa.sync(python, ["-m", "pip", "install", version !== undefined && version !== "" ? `${name}==${version}` : name], {
stdio: "inherit", stdio: "inherit",
}) })
@ -41,12 +48,12 @@ export async function setupPipPack(name: string, version?: string) {
// windows or others // windows or others
try { try {
binDir = join( binDir = join(
(await getExecOutput(`python3 -c "import sys;print(sys.base_exec_prefix);"`)).stdout.trim(), (await getExecOutput(`${python} -c "import sys;print(sys.base_exec_prefix);"`)).stdout.trim(),
"Scripts" "Scripts"
) )
} catch { } catch {
binDir = join( binDir = join(
(await getExecOutput('python -c "import sys;print(sys.base_exec_prefix);"')).stdout.trim(), (await getExecOutput(`${python} -c "import sys;print(sys.base_exec_prefix);"`)).stdout.trim(),
"Scripts" "Scripts"
) )
} }