I have this script running in Node.js, which works fine:
const {PythonShell} = require("python-shell");
PythonShell.run("hello.py", null).then(result => {
try{
console.log(result[0]);
}
catch(err){
console.log("err = ", err);
}
});
The script "hello.py" in Python only has the instruction "print('Hello from Python')", so when running this Node.js script, the output is 'Hello from Python'.
But if I modify it a bit to not use .then(...) but a callback function, then I don't get any output, and it is because it never enters the callback function, as I have corroborated putting a "console.log" in the beginning:
const {PythonShell} = require("python-shell");
PythonShell.run("hello.py", null, function (err, result) {
if (err) throw err;
console.log(result[0]);
});
I have tried putting an options object in the second argument that has the absolute path to the "hello.py" file but it still does not work.
What am I missing?
The version of python-shell is 5.0.0.
You cannot use callbacks with PythonShell.run()
.
Take a look at the source for the run
function, it only takes two arguments. This means your third argument (the callback function) is never seen, as the function isn't written to take one.
The reason you are supposed to use .then()
is because PythonShell.run()
returns a Promise.