在 Windows 中生成 python ENOENT node.js

Jon*_*bek 4 spawn node.js enoent

我创建了一些代码来使用 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)
Run Code Online (Sandbox Code Playgroud)

这是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'))
} )
Run Code Online (Sandbox Code Playgroud)

这是 script.py 文件

import sys

print("work with me please")
sys.stdout.flush()
Run Code Online (Sandbox Code Playgroud)

有很多人有这样的问题,但所有答案似乎都对特定的人过于具体。有些提到了路径变量,有些提到了 npm.cmd,还有一些则完全是别的东西。

我应该如何解决这个特殊情况?

我尝试过npm initnpm install移动代码片段,谷歌搜索并更改 cmd 和目录的范围等等。

小智 6

嘿我有类似的错误:

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

错误:在 processTicksAndRejections (internal/process/task_queues.js:84) 的 onErrorNT (internal/child_process.js:469:16) 处的 Process.ChildProcess._handle.onexit (internal/child_process.js:267:19) 处生成 python ENOENT: 21) 在 ChildProcess 实例上发出“错误”事件:在 Process.ChildProcess._handle.onexit (internal/child_process.js:273:12) 在 onErrorNT (internal/child_process.js:469:16) 在 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}!`))
Run Code Online (Sandbox Code Playgroud)