在Node.js服务器上运行Python脚本时出错

时间:2019-03-11 12:18:51

标签: python node.js

我有以下Python脚本:

import sys 
print("Output from Python") 
print("First name: " + sys.argv[1]) 
print("Last name: " + sys.argv[2]) 

我正在尝试从Node.js服务器调用此脚本,并且我已经在线阅读了两种方法:

第一个是使用PythonShell:

const app = require('express')();
const ps = require('python-shell');

ps.PythonShell.run('hello.py', null, function (err, results) {
   if (err) throw err;
   console.log('finished');
   console.log(results);
});

app.listen(4000);

尝试以这种方式实现它时,出现以下错误:

  

PythonShell.run is not a function

我不确定在这里写的路径是否正确,但是我确定在服务器中写的路径是正确的。

第二种方法是使用child process:

var express = require('express'); 
var app = express(); 

  app.listen(3000, function() { 
  console.log('server running on port 3000'); 
} ) 

app.get('/name', callName); 
function callName(req, res) { 

var spawn = require("child_process").spawn; 
var process = spawn('python',["./hello.py", 
                    req.query.firstname, 
                    req.query.lastname] ); 


process.stdout.on('data', function(data) { 
    res.send(data.toString()); 
  } ) 
 } 

以这种方式尝试时,出现以下错误:

  

错误:在Process.ChildProcess._handle.onexit上生成python ENOENT   (internal / child_process.js:246:19)在onErrorNT   (internal / child_process.js:421:16)在process.internalTickCallback   (内部/进程/next_tick.js:72:19)在以下位置发出了“错误”事件:   Process.ChildProcess._handle.onexit(内部/child_process.js:252:12)   在onErrorNT(internal / child_process.js:421:16)在   process.internalTickCallback(internal / process / next_tick.js:72:19)

有人知道问题出在哪里吗?还是建议其他选择?

2 个答案:

答案 0 :(得分:0)

看起来您提到的答案已经过时了。

尝试一下:

const app = require('express')();
const {PythonShell} = require('python-shell');

PythonShell.run('hello.py', null, function (err, results) {
  if (err) throw err;
  console.log('finished');
  console.log(results);
});

app.listen(4000);

您的第二次尝试在几个地方是错误的。 您可以尝试这种方式:

const express = require('express');
const app = express();
const spawn = require("child_process").spawn;

app.listen(3000, () => {
    console.log('server running on port 3000');
} );

app.get('/name', (req, res) => {

    const firstName = req.query['firstname'],
        lastName = req.query['lastname'];

    if (!firstName || !lastName) {
        res.status(401).send('missing-fields')
    }

    const process = spawn('python',["./hello.py", firstName, lastName] );

    let result = '';

    process.stdout.on('data', data => {
        result += data.toString();
    } );

    process.on('close', code => {
        res.send(result);
    })

} );

答案 1 :(得分:0)

我认为这可能对您的第一种方法有所帮助:

const app = require('express')();
const ps = require('python-shell');

let options = {
  mode: 'text',
  pythonPath: '/usr/bin/python',
  pythonOptions: ['-u'], // get print results in real-time
  scriptPath: '/Users/apple/Desktop/xp',
  args: ['value1', 'value2']
};

ps.PythonShell.run('hello.py', options, function (err, results) {
   if (err) throw err;
   console.log('finished');
   console.log(results);
});

app.listen(4000);

第二种方法代码运行正常,没有错误:

运行:node script.js

它将运行服务器,然后在浏览器上的网址下方打开

http://localhost:3000/name?firstname=Mike&lastname=Will

相关问题