I have the following Python script:
import sys
print("Output from Python")
print("First name: " + sys.argv[1])
print("Last name: " + sys.argv[2])
I am trying to call this script from a Node.js server and I have read online two ways of doing this:
The first is by using 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);
When trying to achive it this way I get the following error:
PythonShell.run is not a function
.
I'm not sure if the path I wrote here is the correct one but I made sure the path I wrote in the server was correct.
The second way is by using 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());
} )
}
When trying this way I get the following error:
Error: spawn python ENOENT at Process.ChildProcess._handle.onexit (internal/child_process.js:246:19) at onErrorNT (internal/child_process.js:421:16) at process.internalTickCallback (internal/process/next_tick.js:72:19) Emitted 'error' event at: at Process.ChildProcess._handle.onexit (internal/child_process.js:252:12) at onErrorNT (internal/child_process.js:421:16) at process.internalTickCallback (internal/process/next_tick.js:72:19)
Does anyone know what the problem is? Or maybe suggest a different option?