在 windows 中生成 python ENOENT node.js

spawn python ENOENT node.js in windows

我为我兄弟创建了一些代码,希望使用他 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 和目录的范围等进行资助。

嘿,我有一个类似的问题,这成功了,当我添加 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);
});

我有类似的错误:

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

Error: spawn python ENOENT at Process.ChildProcess._handle.onexit (internal/child_process.js:267:19) at onErrorNT (internal/child_process.js:469:16) at processTicksAndRejections (internal/process/task_queues.js:84:21) Emitted 'error' event on ChildProcess instance at: at Process.ChildProcess._handle.onexit (internal/child_process.js:273:12) at onErrorNT (internal/child_process.js:469:16) at processTicksAndRejections (internal/process/task_queues.js:84:21) { errno: 'ENOENT', code: 'ENOENT', syscall: 'spawn python', path: '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}!`))