24 lines
1.3 KiB
JavaScript
24 lines
1.3 KiB
JavaScript
import {spawnSync} from 'node:child_process';
|
||
import path from 'node:path';
|
||
|
||
export function selectPython(requested) {
|
||
const command = requested || (process.env.CONDA_PREFIX
|
||
? path.join(process.env.CONDA_PREFIX, process.platform === 'win32' ? 'python.exe' : 'bin/python')
|
||
: 'python');
|
||
const probe = spawnSync(command, ['-c', [
|
||
'import json, sys',
|
||
'from pathlib import Path',
|
||
"print(json.dumps({'executable': sys.executable, 'name': Path(sys.prefix).name, 'conda': (Path(sys.prefix) / 'conda-meta').is_dir()}))",
|
||
].join('\n')], {encoding: 'utf8', windowsHide: true});
|
||
let runtime;
|
||
try { if (probe.status === 0) runtime = JSON.parse(probe.stdout.trim()); } catch { /* Report an actionable environment error below. */ }
|
||
if (!runtime?.conda || runtime.name !== 'ai-spine') {
|
||
throw new Error('本项目固定使用 Conda 环境 ai-spine。请先 conda activate ai-spine,或用 conda run -n ai-spine node ...;--python 也必须指向该环境的解释器');
|
||
}
|
||
const dependencies = spawnSync(runtime.executable, ['-c', 'import cv2, PIL, numpy'], {stdio: 'ignore', windowsHide: true});
|
||
if (dependencies.status !== 0) {
|
||
throw new Error('ai-spine 环境缺少或无法加载 Python 依赖。请在 AI_spine 目录执行:conda run -n ai-spine python -m pip install -r requirements.txt');
|
||
}
|
||
return runtime.executable;
|
||
}
|