Cadell Teng
Cadell Teng

Reputation: 244

python-shell from Node.js not executing python scripts

I'm trying to learn JavaScript. Was previously coding in Python. I like to link some of my Python programs to run in conjunction with Node.js. Using codes from python-shell to run my Python scripts but encountered the following error which I can't even begin to understand:

/Users/cadellteng/AtomProjects/useSpawn/node_modules/python-shell/index.js:217
                return callback(err);
                       ^

TypeError: callback is not a function
    at PythonShell._endCallback (/Users/cadellteng/AtomProjects/useSpawn/node_modules/python-shell/index.js:217:24)
    at terminateIfNeeded (/Users/cadellteng/AtomProjects/useSpawn/node_modules/python-shell/index.js:158:39)
    at ChildProcess.<anonymous> (/Users/cadellteng/AtomProjects/useSpawn/node_modules/python-shell/index.js:131:13)
    at ChildProcess.emit (events.js:316:20)
    at Process.ChildProcess._handle.onexit (internal/child_process.js:275:12)

I'm only trying to run a simple node and python script containing 5 and 2 lines respectively. Please see my code below and let me know if I'm doing anything wrong:

usingPythonShell.js

let {PythonShell} = require('python-shell');

PythonShell.run('hello.py', () => {
  console.log('finished');
});

hello.py

name = input('What is your name?\n>>> ') # John Doe
print('Hello '+ name +'!') # expects output as 'Hello John Doe!'

I tried to put my python inside a main() and it doesn't seem to work as well.

I'm confident the python script works because it executed fine when I executed it from the terminal.

Upvotes: 0

Views: 1592

Answers (1)

9000
9000

Reputation: 40884

Look at the documentation.

The arguments to PythonShell.run are a script name, an options object, and a callback. So the callback is the third argument.

In your case you pass the callback where the options should be. The third argument remains undefined, and indeed it's not a function.

Upvotes: 1

Related Questions