fix(cli): text-to-speech ignores a foreign PYTHONPATH when it looks for kokoro-onnx (#6769)

* fix(cli): text-to-speech ignores a foreign PYTHONPATH when it looks for kokoro-onnx

* fix(cli): shorten the pythonEnv comment

* fix(cli): text-to-speech keeps a deliberate PYTHONPATH unless the packages fail with it

* fix(cli): drop the python.ts comments the names already say
This commit is contained in:
Miguel Ángel
2026-10-30 02:25:39 +00:00
committed by GitHub
parent 471d60a8ac
commit d1c0593717
4 changed files with 123 additions and 26 deletions
+26 -15
View File
@@ -77,19 +77,38 @@ export function describeRejectedPythonOverride(): string | null {
return `HYPERFRAMES_PYTHON="${override}" was rejected: ${validation.reason}`;
}
/** True if `import <pkg>` succeeds — actually executes the module. */
export function hasPythonPackage(python: string, pkg: string): boolean {
function withoutPythonPath(): NodeJS.ProcessEnv {
const { PYTHONPATH: _foreign, ...env } = process.env;
return env;
}
function runs(python: string, code: string, env: NodeJS.ProcessEnv): boolean {
try {
execFileSync(python, ["-c", `import ${pkg}`], {
stdio: ["pipe", "pipe", "pipe"],
timeout: 10_000,
});
execFileSync(python, ["-c", code], { stdio: ["pipe", "pipe", "pipe"], timeout: 10_000, env });
return true;
} catch {
return false;
}
}
function environmentThatRuns(python: string, code: string): NodeJS.ProcessEnv | null {
return [process.env, withoutPythonPath()].find((env) => runs(python, code, env)) ?? null;
}
export function workingPythonEnv(
python: string,
modules: string[],
extra: NodeJS.ProcessEnv = {},
): NodeJS.ProcessEnv | null {
const env = environmentThatRuns(python, `import ${modules.join(", ")}`);
return env && { ...env, ...extra };
}
/** True if `import <pkg>` succeeds — actually executes the module. */
export function hasPythonPackage(python: string, pkg: string): boolean {
return environmentThatRuns(python, `import ${pkg}`) !== null;
}
/**
* True if every module is installed, checked via `importlib.util.find_spec`
* so heavy packages (torch) are never imported — fast enough for a preflight.
@@ -100,13 +119,5 @@ export function hasPythonModules(modules: string[]): boolean {
if (!python) return false;
const list = JSON.stringify(modules);
const probe = `import importlib.util,sys; sys.exit(0 if all(importlib.util.find_spec(m) for m in ${list}) else 1)`;
try {
execFileSync(python, ["-c", probe], {
stdio: ["pipe", "pipe", "pipe"],
timeout: 10_000,
});
return true;
} catch {
return false;
}
return environmentThatRuns(python, probe) !== null;
}