How to call python script from NodeJs

There are multiple ways of doing this.

  • first way is by doing npm install python-shell

and here’s the code

var PythonShell = require('python-shell');
//you can use error handling to see if there are any errors
PythonShell.run('my_script.py', options, function (err, results) { 
//your code

you can send a message to python shell using
pyshell.send('hello');

you can find the API reference here-
https://github.com/extrabacon/python-shell

a few more references –
https://www.npmjs.com/package/python

if you want to use service-oriented architecture –
http://ianhinsdale.com/code/2013/12/08/communicating-between-nodejs-and-python/

Leave a Comment