1

我为我的兄弟创建了一些代码,希望使用他的 node.js 后端的 python 函数。在我的 ubuntu 计算机上运行它时,它可以工作 - 但是!在他的 Windows 机器上运行代码时,它会给出这个堆栈跟踪。

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 和目录的范围等进行资助。

4

2 回答 2

2

嘿,我遇到了类似的问题,这成功了,当我添加 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);
});
于 2020-02-03T11:55:12.663 回答
2

我有类似的错误:

events.js:292 抛出错误;// 未处理的“错误”事件 ^

错误:在 processTicksAndRejections (internal/process/task_queues.js:84: 21)在 ChildProcess 实例上发出“错误”事件:在 Process.ChildProcess._handle.onexit (internal/child_process.js:273:12) 在 onErrorNT (internal/child_process.js:469:16) at processTicksAndRejections (internal/process /task_queues.js:84:21) { errno:'ENOENT',代码:'ENOENT',系统调用:'spawn python',路径:'python',spawnargs:['/home/NodeJsRunPython/script2.py']}

此脚本改编自https://medium.com/swlh/run-python-script-from-node-js-and-send-data-to-browser-15677fcf199f

我在“python3”中更改“python”

const python = spawn('python3', [__dirname +'/script2.py']);

对我来说它有效:

const express = require('express')
const {spawn} = require('child_process');
const app = express()
const port = 3000

app.get('/', (req, res) => {
 
 var dataToSend;
 // spawn new child process to call the python script
 const python = spawn('python', [__dirname +'/script2.py']);
 // collect data from script
  python.stdout.on('data', function (data) {
  console.log('Pipe data from python script ...');
  dataToSend = data.toString();
 });
 // in close event we are sure that stream from child process is closed
 python.on('close', (code) => {
 console.log(`child process close all stdio with code ${code}`);
 // send data to browser
 res.send(dataToSend)
 });
 
})
app.listen(port, () => console.log(`Example app listening on port 
${port}!`))

于 2020-11-25T15:47:29.573 回答