在Windows中生成python ENOENT node.js

问题描述 投票:0回答:1

我为我的兄弟创建了一些代码,想要从他的node.js后端使用python函数。在我的ubuntu计算机上运行它时,它可以工作-但是!在他的Windows机器上运行代码时,它会提供此stacktrace。

events.js:174
      throw er; // Unhandled 'error' event
      ^

Error: spawn python 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:757:11)
    at startup (internal/bootstrap/node.js:283:19)
    at bootstrapNodeJSCore (internal/bootstrap/node.js:622: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:622:3)

这是node.js文件

const spawn = require("child_process").spawn;
const pythonProcess = exec('python',["./script.py", 2, 4]);

pythonProcess.stdout.on('data', function(data) {
    console.log(data.toString('utf-8'))
} )

这是script.py文件

import sys

print("work with me please")
sys.stdout.flush()

[有很多这样的人,但是对于特定的人来说,所有答案似乎都过分具体。有些提到路径变量,有些提到npm.cmd,有些提到第三位。

我应该如何处理这个特殊情况?


编辑:

我已经尝试过npm init,npm install,为移动代码段进行谷歌搜索以及更改cmd和目录的范围等等。

python node.js spawn enoent
1个回答
0
投票

嘿,我有一个类似的问题,这可以解决问题,当我添加pythonPath:'python'

时,此问题已得到解决。
const { PythonShell } = require('python-shell');

let options = {
    mode: 'text',
    pythonPath: 'python',
    pythonOptions: ['-u'], // get print results in real-time
    scriptPath: 'path',
    args: ['arg1', 'arg2']
};

PythonShell.run('scraper.py', options, function(err, results) {
    if (err) console.log(err);
    // results is an array consisting of messages collected during execution
    console.log('results: %j', results);
});
© www.soinside.com 2019 - 2024. All rights reserved.