gpt4 book ai didi

javascript - 从 Electron 应用程序运行python脚本时如何修复 “Error: spawn py ENOENT”错误?

转载 作者:行者123 更新时间:2023-12-03 12:25:54 27 4
gpt4 key购买 nike

我正在尝试从Windows 10中的electronic app运行一些python脚本。我尝试运行的示例代码是:

let {PythonShell} = require('python-shell')
PythonShell.run('test.py', function (err, results) {
if (err) throw err;
console.log('test.py finished.');
console.log('results', results);
});

该代码有望运行test.py,其中包含一个简单的print语句。但是终端显示以下错误日志:
throw er; // Unhandled 'error' event
^

Error: spawn py ENOENT
at Process.ChildProcess._handle.onexit (internal/child_process.js:240:19)
at onErrorNT (internal/child_process.js:415:16)
at process._tickCallback (internal/process/next_tick.js:63:19)
at Function.Module.runMain (internal/modules/cjs/loader.js:745:11)
at startup (internal/bootstrap/node.js:283:19)
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)
Emitted 'error' event at:
at Process.ChildProcess._handle.onexit (internal/child_process.js:246:12)
at onErrorNT (internal/child_process.js:415:16)
[... lines matching original stack trace ...]
at bootstrapNodeJSCore (internal/bootstrap/node.js:743:3)

最佳答案

Looks like the Python executable is not available from your node script, which is almost always the case in Windows. Either add the python executable to your PATH variable, or specify the executable path in the options. See constructor options for more details.



使用 https://github.com/extrabacon/python-shell#pythonshellscript-options-constructor

设置pythonPath并尝试。
let options = {
pythonPath: 'C:\\python27\\python',
};

let {PythonShell} = require('python-shell')
PythonShell.run('test.py', options, function (err, results) {
if (err) throw err;
console.log('test.py finished.');
console.log('results', results);
});

https://github.com/extrabacon/python-shell/issues/3#issuecomment-52174564

关于javascript - 从 Electron 应用程序运行python脚本时如何修复 “Error: spawn py ENOENT”错误?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58390884/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com